From 9c6da385be908d454ead2056de9b9c31546411ef Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 16 Mar 2021 22:13:50 +0200 Subject: Translation: Remember last used languages --- src/ui/translation.c | 56 ++++++++++++++++++++----------------------- src/ui/util.c | 67 ++++++++++++++++++++++++++++++++++------------------ src/ui/util.h | 1 + 3 files changed, 70 insertions(+), 54 deletions(-) (limited to 'src/ui') diff --git a/src/ui/translation.c b/src/ui/translation.c index e5059f38..bbc1763e 100644 --- a/src/ui/translation.c +++ b/src/ui/translation.c @@ -151,13 +151,6 @@ iDefineTypeConstructionArgs(Translation, (iDocumentWidget *doc), doc) static const char * translationServiceHost = "xlt.skyjake.fi"; static const uint16_t translationServicePort = 443; -//static const char *doubleArrowSymbol = "\U0001f192"; //"\u20e2"; /* prevent getting mangled */ -//static const char *tripleBacktickSymbol = "\U0001f1a9"; //\u20e3"; -//static const char *h1Symbol = "\U0001f19d"; -//static const char *h2Symbol = "\U0001f19e"; -//static const char *h3Symbol = "\U0001f19f"; -//static const char *bulletSymbol = "\n\U0001f196"; - static iString *quote_String_(const iString *d) { iString *quot = new_String(); iConstForEach(String, i, d) { @@ -292,13 +285,19 @@ static uint32_t animate_Translation_(uint32_t interval, iAny *ptr) { } void submit_Translation(iTranslation *d) { + iAssert(status_TlsRequest(d->request) != submitted_TlsRequestStatus); /* Check the selected languages from the dialog. */ const char *idFrom = languageId_String(text_LabelWidget(findChild_Widget(d->dlg, "xlt.from"))); const char *idTo = languageId_String(text_LabelWidget(findChild_Widget(d->dlg, "xlt.to"))); - iAssert(status_TlsRequest(d->request) != submitted_TlsRequestStatus); - iBlock *json = collect_Block(new_Block(0)); + /* Remember these in Preferences. */ + postCommandf_App("translation.languages from:%d to:%d", + languageIndex_CStr(idFrom), + languageIndex_CStr(idTo)); + iBlock * json = collect_Block(new_Block(0)); iString *docSrc = collectNew_String(); - /* TODO: Strip all markup and remember it. These are reapplied when reading response. */ { + /* The translation engine doesn't preserve Gemtext markup so we'll strip all of it and + remember each line's type. These are reapplied when reading the response. Newlines seem + to be preserved pretty well. */ { iRangecc line = iNullRange; while (nextSplit_Rangecc( range_String(source_GmDocument(document_DocumentWidget(d->doc))), "\n", &line)) { @@ -317,28 +316,25 @@ void submit_Translation(iTranslation *d) { appendRange_String(docSrc, cleanLine); } } -// replace_String(docSrc, "=>", doubleArrowSymbol); -// replace_String(docSrc, "```", tripleBacktickSymbol); -// replace_String(docSrc, "###", h3Symbol); -// replace_String(docSrc, "##", h2Symbol); -// replace_String(docSrc, "#", h1Symbol); -// replace_String(docSrc, "\n*", bulletSymbol); printf_Block(json, "{\"q\":\"%s\",\"source\":\"%s\",\"target\":\"%s\"}", cstrCollect_String(quote_String_(docSrc)), idFrom, idTo); iBlock *msg = collect_Block(new_Block(0)); - printf_Block(msg, "POST /translate HTTP/1.1\r\n" - "Host: xlt.skyjake.fi\r\n" - "Connection: close\r\n" - "Content-Type: application/json; charset=utf-8\r\n" - "Content-Length: %zu\r\n\r\n", size_Block(json)); + printf_Block(msg, + "POST /translate HTTP/1.1\r\n" + "Host: %s\r\n" + "Connection: close\r\n" + "Content-Type: application/json; charset=utf-8\r\n" + "Content-Length: %zu\r\n\r\n", + translationServiceHost, + size_Block(json)); append_Block(msg, json); setContent_TlsRequest(d->request, msg); submit_TlsRequest(d->request); d->startTime = SDL_GetTicks(); - d->timer = SDL_AddTimer(1000 / 30, animate_Translation_, d); + d->timer = SDL_AddTimer(1000 / 30, animate_Translation_, d); } static void setFailed_Translation_(iTranslation *d, const char *msg) { @@ -356,8 +352,8 @@ static iBool processResult_Translation_(iTranslation *d) { return iFalse; } iBlock *resultData = collect_Block(readAll_TlsRequest(d->request)); - printf("result(%zu):\n%s\n", size_Block(resultData), cstr_Block(resultData)); - fflush(stdout); +// printf("result(%zu):\n%s\n", size_Block(resultData), cstr_Block(resultData)); +// fflush(stdout); iRegExp *pattern = iClob(new_RegExp(".*translatedText\":\"(.*)\"\\}", caseSensitive_RegExpOption)); iRegExpMatch m; init_RegExpMatch(&m); @@ -401,12 +397,6 @@ static iBool processResult_Translation_(iTranslation *d) { appendRange_String(marked, cleanLine); lineIndex++; } -// replace_String(translation, tripleBacktickSymbol, "```"); -// replace_String(translation, doubleArrowSymbol, "=>"); -// replace_String(translation, h3Symbol, "### "); -// replace_String(translation, h2Symbol, "## "); -// replace_String(translation, h1Symbol, "# "); -// replace_String(translation, bulletSymbol, "\n* "); setSource_DocumentWidget(d->doc, marked); postCommand_App("sidebar.update"); delete_String(translation); @@ -419,7 +409,11 @@ static iBool processResult_Translation_(iTranslation *d) { } static iLabelWidget *acceptButton_Translation_(const iTranslation *d) { - return (iLabelWidget *) lastChild_Widget(findChild_Widget(d->dlg, "dialogbuttons")); + iWidget *buttonParent = findChild_Widget(d->dlg, "dialogbuttons"); +// if (!buttonParent) { +// buttonParent = findChild_Widget(d->dlg, "panel.back"); +// } + return (iLabelWidget *) lastChild_Widget(buttonParent); } iBool handleCommand_Translation(iTranslation *d, const char *cmd) { diff --git a/src/ui/util.c b/src/ui/util.c index c1e27751..400e65d4 100644 --- a/src/ui/util.c +++ b/src/ui/util.c @@ -1078,6 +1078,12 @@ static iWidget *makeValuePadding_(iWidget *value) { } void finalizeSheet_Widget(iWidget *sheet) { + /* The sheet contents are completely rearranged and restyled on a phone. + We'll set up a linear fullscreen arrangement of the widgets. Sheets are already + scrollable so they can be taller than the display. In hindsight, it may have been + easier to create phone versions of each dialog, but at least this works with any + future changes to the UI (..."works"). At least this way it is possible to enforce + a consistent styling. */ if (deviceType_App() == phone_AppDeviceType && parent_Widget(sheet) == get_Window()->root) { if (~flags_Widget(sheet) & keepOnTop_WidgetFlag) { /* Already finalized. */ @@ -1085,11 +1091,6 @@ void finalizeSheet_Widget(iWidget *sheet) { postRefresh_App(); return; } - /* The sheet contents are completely rearranged on a phone. We'll set up a linear - fullscreen arrangement of the widgets. Sheets are already scrollable so they - can be taller than the display. In hindsight, it may have been easier to - create phone versions of each dialog, but at least this works with any future - changes to the UI (..."works"). */ setFlags_Widget(sheet, keepOnTop_WidgetFlag | parentCannotResize_WidgetFlag | @@ -1366,6 +1367,9 @@ void finalizeSheet_Widget(iWidget *sheet) { /* Pick up the dialog buttons for the navbar. */ iWidget *buttons = findChild_Widget(sheet, "dialogbuttons"); iLabelWidget *cancel = findMenuItem_Widget(buttons, "cancel"); +// if (!cancel) { +// cancel = findMenuItem_Widget(buttons, "translation.cancel"); +// } if (cancel) { updateText_LabelWidget(back, text_LabelWidget(cancel)); setCommand_LabelWidget(back, command_LabelWidget(cancel)); @@ -2223,6 +2227,7 @@ static const iMenuItem languages[] = { }; static iBool translationHandler_(iWidget *dlg, const char *cmd) { + iUnused(dlg); if (equal_Command(cmd, "xlt.lang")) { iLabelWidget *menuItem = pointer_Command(cmd); iWidget *button = parent_Widget(parent_Widget(menuItem)); @@ -2242,6 +2247,15 @@ const char *languageId_String(const iString *menuItemLabel) { return ""; } +int languageIndex_CStr(const char *langId) { + iForIndices(i, languages) { + if (equal_Rangecc(range_Command(languages[i].command, "id"), langId)) { + return i; + } + } + return -1; +} + iWidget *makeTranslation_Widget(iWidget *parent) { iWidget *dlg = makeSheet_Widget("xlt"); setFlags_Widget(dlg, keepOnTop_WidgetFlag, iFalse); @@ -2254,25 +2268,32 @@ iWidget *makeTranslation_Widget(iWidget *parent) { iWidget *page; addChild_Widget(dlg, iClob(page = makeTwoColumnWidget_(&headings, &values))); setId_Widget(page, "xlt.langs"); - addChild_Widget(headings, iClob(makeHeading_Widget("From:"))); iLabelWidget *fromLang, *toLang; - setId_Widget(addChildFlags_Widget(values, - iClob(fromLang = makeMenuButton_LabelWidget( - "Portuguese", languages, iElemCount(languages))), - alignLeft_WidgetFlag), - "xlt.from"); - updateTextCStr_LabelWidget(fromLang, "French"); /* TODO: Check source media type; remember last use. */ - setBackgroundColor_Widget(findChild_Widget(as_Widget(fromLang), "menu"), - uiBackgroundMenu_ColorId); - addChild_Widget(headings, iClob(makeHeading_Widget("To:"))); - setId_Widget(addChildFlags_Widget(values, - iClob(toLang = makeMenuButton_LabelWidget( - "Portuguese", languages, iElemCount(languages))), - alignLeft_WidgetFlag), - "xlt.to"); - setBackgroundColor_Widget(findChild_Widget(as_Widget(toLang), "menu"), - uiBackgroundMenu_ColorId); - updateTextCStr_LabelWidget(toLang, "English"); /* TODO: User preference. */ + /* Source language. */ { + addChild_Widget(headings, iClob(makeHeading_Widget("From:"))); + setId_Widget( + addChildFlags_Widget(values, + iClob(fromLang = makeMenuButton_LabelWidget( + "Portuguese", languages, iElemCount(languages))), + alignLeft_WidgetFlag), + "xlt.from"); + iWidget *langMenu = findChild_Widget(as_Widget(fromLang), "menu"); + updateText_LabelWidget(fromLang, + text_LabelWidget(child_Widget(langMenu, prefs_App()->langFrom))); + setBackgroundColor_Widget(langMenu, uiBackgroundMenu_ColorId); + } + /* Target language. */ { + addChild_Widget(headings, iClob(makeHeading_Widget("To:"))); + setId_Widget(addChildFlags_Widget(values, + iClob(toLang = makeMenuButton_LabelWidget( + "Portuguese", languages, iElemCount(languages))), + alignLeft_WidgetFlag), + "xlt.to"); + iWidget *langMenu = findChild_Widget(as_Widget(toLang), "menu"); + setBackgroundColor_Widget(langMenu, uiBackgroundMenu_ColorId); + updateText_LabelWidget(toLang, + text_LabelWidget(child_Widget(langMenu, prefs_App()->langTo))); + } addChild_Widget(dlg, iClob(makePadding_Widget(lineHeight_Text(uiLabel_FontId)))); addChild_Widget( dlg, diff --git a/src/ui/util.h b/src/ui/util.h index 251683f5..35e10c6f 100644 --- a/src/ui/util.h +++ b/src/ui/util.h @@ -226,3 +226,4 @@ iWidget * makeFeedSettings_Widget (uint32_t bookmarkId); iWidget * makeTranslation_Widget (iWidget *parent); const char * languageId_String (const iString *menuItemLabel); +int languageIndex_CStr (const char *langId); -- cgit v1.2.3