From 17c37c3dfabc89c10888f1d39fc7b54d0f7b19dd Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 25 Jul 2020 12:43:42 +0300 Subject: Reload, Stop, and Home navbar buttons --- src/app.c | 31 +++++++++++++++++++++++++------ src/ui/documentwidget.c | 17 +++++++++++++++++ src/ui/documentwidget.h | 3 ++- src/ui/labelwidget.c | 2 ++ src/ui/window.c | 26 +++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/app.c b/src/app.c index 44c4c524..e0037f0c 100644 --- a/src/app.c +++ b/src/app.c @@ -222,12 +222,7 @@ static void init_App_(iApp *d, int argc, char **argv) { loadHistory_App_(d); d->window = new_Window(); /* Widget state init. */ { - iString *homePath = newCStr_String(dataDir_App_); - clean_Path(homePath); - append_Path(homePath, &iStringLiteral("home.gmi")); - prependCStr_String(homePath, "file://"); - postCommandf_App("open url:%s", cstr_String(homePath)); - delete_String(homePath); + postCommand_App("navigate.home"); } } @@ -448,6 +443,21 @@ iBool handleCommand_App(const char *cmd) { printHistory_App_(d); setUrl_DocumentWidget(findChild_Widget(root, "document"), url); } + else if (equal_Command(cmd, "document.request.cancelled")) { + /* TODO: How should cancelled requests be treated in the history? */ +#if 0 + if (d->historyPos == 0) { + iHistoryItem *item = historyItem_App_(d, 0); + if (item) { + /* Pop this cancelled URL off history. */ + deinit_HistoryItem(item); + popBack_Array(&d->history); + printHistory_App_(d); + } + } +#endif + return iFalse; + } else if (equal_Command(cmd, "quit")) { SDL_Event ev; ev.type = SDL_QUIT; @@ -487,6 +497,15 @@ iBool handleCommand_App(const char *cmd) { } return iTrue; } + else if (equal_Command(cmd, "navigate.home")) { + iString *homePath = newCStr_String(dataDir_App_); + clean_Path(homePath); + append_Path(homePath, &iStringLiteral("home.gmi")); + prependCStr_String(homePath, "file://"); + postCommandf_App("open url:%s", cstr_String(homePath)); + delete_String(homePath); + return iTrue; + } else { return iFalse; } diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 4cd0ac18..030a323b 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -148,6 +148,7 @@ static void fetch_DocumentWidget_(iDocumentWidget *d) { iRelease(d->request); d->request = NULL; } + postCommand_Widget(as_Widget(d), "document.request.started"); d->state = fetching_DocumentState; set_Atomic(&d->isSourcePending, iFalse); d->request = new_GmRequest(); @@ -171,6 +172,10 @@ void setUrl_DocumentWidget(iDocumentWidget *d, const iString *url) { delete_String(newUrl); } +iBool isRequestOngoing_DocumentWidget(const iDocumentWidget *d) { + return d->state == fetching_DocumentState || d->state == receivedPartialResponse_DocumentState; +} + static void scroll_DocumentWidget_(iDocumentWidget *d, int offset) { d->scrollY += offset; if (d->scrollY < 0) { @@ -278,6 +283,18 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e postCommandf_App("document.changed url:%s", cstr_String(d->url)); return iTrue; } + else if (isCommand_UserEvent(ev, "document.stop")) { + if (d->request) { + postCommandf_App("document.request.cancelled url:%s", cstr_String(d->url)); + iReleasePtr(&d->request); + d->state = ready_DocumentState; + } + return iTrue; + } + else if (isCommand_UserEvent(ev, "document.reload")) { + fetch_DocumentWidget_(d); + return iTrue; + } else if (isCommand_Widget(w, ev, "scroll.moved")) { d->scrollY = arg_Command(command_UserEvent(ev)); updateVisible_DocumentWidget_(d); diff --git a/src/ui/documentwidget.h b/src/ui/documentwidget.h index 02b75e69..dfd342af 100644 --- a/src/ui/documentwidget.h +++ b/src/ui/documentwidget.h @@ -5,4 +5,5 @@ iDeclareWidgetClass(DocumentWidget) iDeclareObjectConstruction(DocumentWidget) -void setUrl_DocumentWidget (iDocumentWidget *d, const iString *url); +void setUrl_DocumentWidget (iDocumentWidget *, const iString *url); +iBool isRequestOngoing_DocumentWidget (const iDocumentWidget *); diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c index 341c2f0e..f24e2573 100644 --- a/src/ui/labelwidget.c +++ b/src/ui/labelwidget.c @@ -264,10 +264,12 @@ void setText_LabelWidget(iLabelWidget *d, const iString *text) { void updateText_LabelWidget(iLabelWidget *d, const iString *text) { set_String(&d->label, text); + refresh_Widget(&d->widget); } void updateTextCStr_LabelWidget(iLabelWidget *d, const char *text) { setCStr_String(&d->label, text); + refresh_Widget(&d->widget); } void setTextCStr_LabelWidget(iLabelWidget *d, const char *text) { diff --git a/src/ui/window.c b/src/ui/window.c index 8236b9e6..6196a638 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -74,6 +74,9 @@ static const iMenuItem editMenuItems[] = { static const iMenuItem viewMenuItems[] = { }; +static const char *reloadCStr_ = "\u25cb"; +static const char *stopCStr_ = orange_ColorEscape "\u00d7"; + static iBool handleNavBarCommands_(iWidget *navBar, const char *cmd) { if (equal_Command(cmd, "input.ended")) { iInputWidget *url = findChild_Widget(navBar, "url"); @@ -88,8 +91,27 @@ static iBool handleNavBarCommands_(iWidget *navBar, const char *cmd) { iInputWidget *url = findWidget_App("url"); setTextCStr_InputWidget(url, valuePtr_Command(cmd, "url")); setTitle_Window(get_Window(), text_InputWidget(url)); + updateTextCStr_LabelWidget(findChild_Widget(navBar, "reload"), reloadCStr_); + return iFalse; + } + else if (equal_Command(cmd, "document.request.cancelled")) { + updateTextCStr_LabelWidget(findChild_Widget(navBar, "reload"), reloadCStr_); return iFalse; } + else if (equal_Command(cmd, "document.request.started")) { + updateTextCStr_LabelWidget(findChild_Widget(navBar, "reload"), stopCStr_); + return iFalse; + } + else if (equal_Command(cmd, "navigate.reload")) { + iDocumentWidget *doc = findWidget_App("document"); + if (isRequestOngoing_DocumentWidget(doc)) { + postCommand_App("document.stop"); + } + else { + postCommand_App("document.reload"); + } + return iTrue; + } return iFalse; } @@ -120,7 +142,9 @@ static void setupUserInterface_Window(iWindow *d) { setId_Widget(as_Widget(url), "url"); setTextCStr_InputWidget(url, "gemini://"); addChildFlags_Widget(navBar, iClob(url), expand_WidgetFlag); - addChild_Widget(navBar, iClob(new_LabelWidget("x", 0, 0, "navigate.reload"))); + setId_Widget( + addChild_Widget(navBar, iClob(new_LabelWidget(reloadCStr_, 0, 0, "navigate.reload"))), + "reload"); } addChildFlags_Widget(div, iClob(new_DocumentWidget()), expand_WidgetFlag); -- cgit v1.2.3