implement navigation back/forward buttons

Tue, 30 Jan 2024 16:01:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 30 Jan 2024 16:01:44 +0100
changeset 15
78684a24dc52
parent 14
872d570ae843
child 16
f2e79b69d233

implement navigation back/forward buttons

application/application.h file | annotate | diff | comparison | revisions
application/davcontroller.c file | annotate | diff | comparison | revisions
application/davcontroller.h file | annotate | diff | comparison | revisions
application/window.c file | annotate | diff | comparison | revisions
application/window.h file | annotate | diff | comparison | revisions
--- a/application/application.h	Tue Jan 30 14:01:04 2024 +0100
+++ b/application/application.h	Tue Jan 30 16:01:44 2024 +0100
@@ -33,10 +33,13 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <libidav/webdav.h>
 #include <libidav/config.h>
 
+#include <cx/linked_list.h>
+
 #ifdef	__cplusplus
 extern "C" {
 #endif
@@ -59,6 +62,10 @@
 
 	DavResource *current;
 
+	CxList *navigation_stack;
+	bool navstack_enabled;
+	int navstack_pos;
+
 	/*
 	 * path textfield value 
 	 */
--- a/application/davcontroller.c	Tue Jan 30 14:01:04 2024 +0100
+++ b/application/davcontroller.c	Tue Jan 30 16:01:44 2024 +0100
@@ -38,6 +38,10 @@
     UiContext *ctx = ui_document_context(doc);
     doc->ctx = ctx;
 
+    doc->navigation_stack = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    doc->navstack_enabled = true;
+    doc->navstack_pos = 0;
+
     doc->dav_queue = ui_threadpool_create(1);
 
     doc->path = ui_string_new(ctx, "path");
@@ -149,6 +153,8 @@
     ui_threadpool_job(browser->dav_queue, ui, browser_query_path, query, browser_query_finished, query);
 
     window_progress(ui->window, 1);
+
+    davbrowser_add2navstack(browser, browser->repo_base, path);
 }
 
 void davbrowser_query_url(UiObject *ui, DavBrowser *browser, const char *url) {
@@ -165,3 +171,43 @@
 
     // TODO:
 }
+
+void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path) {
+    if (browser->navstack_enabled) {
+        for (int i = 0; i < browser->navstack_pos; i++) {
+            char *nav_url = cxListAt(browser->navigation_stack, 0);
+            cxListRemove(browser->navigation_stack, 0);
+            free(nav_url);
+        }
+        browser->navstack_pos = 0;
+
+        char *nav_url = util_concat_path(base, path);
+        cxListInsert(browser->navigation_stack, 0, nav_url);
+
+        if (browser->navigation_stack->size > DAVBROWSER_MAX_NAVLIST) {
+            char *nav = cxListAt(browser->navigation_stack, browser->navigation_stack->size - 1);
+            free(nav);
+            cxListRemove(browser->navigation_stack, browser->navigation_stack->size - 1);
+        }
+    }
+}
+
+void davbrowser_navigation_back(UiObject *ui, DavBrowser *browser) {
+    if (browser->navstack_pos+1 < browser->navigation_stack->size) {
+        browser->navstack_pos++;
+        char *nav_url = cxListAt(browser->navigation_stack, browser->navstack_pos);
+        browser->navstack_enabled = false;
+        davbrowser_query_url(ui, browser, nav_url);
+        browser->navstack_enabled = true;
+    }
+}
+
+void davbrowser_navigation_forward(UiObject *ui, DavBrowser *browser) {
+    if (browser->navstack_pos > 0) {
+        browser->navstack_pos--;
+        char *nav_url = cxListAt(browser->navigation_stack, browser->navstack_pos);
+        browser->navstack_enabled = false;
+        davbrowser_query_url(ui, browser, nav_url);
+        browser->navstack_enabled = true;
+    }
+}
--- a/application/davcontroller.h	Tue Jan 30 14:01:04 2024 +0100
+++ b/application/davcontroller.h	Tue Jan 30 16:01:44 2024 +0100
@@ -39,6 +39,8 @@
 extern "C" {
 #endif
 
+#define DAVBROWSER_MAX_NAVLIST 128
+
 DavBrowser* davbrowser_create(UiObject *toplevel);
 
 void davbrowser_set_collection(UiObject *ui, DavBrowser *browser, DavResource *collection);
@@ -49,6 +51,11 @@
 
 void davbrowser_query_url(UiObject *ui, DavBrowser *browser, const char *url);
 
+void davbrowser_add2navstack(DavBrowser *browser, const char *base, const char *path);
+
+void davbrowser_navigation_back(UiObject *ui, DavBrowser *browser);
+void davbrowser_navigation_forward(UiObject *ui, DavBrowser *browser);
+
 
 #ifdef	__cplusplus
 }
--- a/application/window.c	Tue Jan 30 14:01:04 2024 +0100
+++ b/application/window.c	Tue Jan 30 16:01:44 2024 +0100
@@ -56,8 +56,8 @@
 
 	// navigation bar
 	ui_hbox(obj, .fill = UI_OFF, .margin = 8) {
-		ui_button(obj, .icon = "Back");
-		ui_button(obj, .icon = "Forward");
+		ui_button(obj, .icon = "Back", .onclick = action_go_back);
+		ui_button(obj, .icon = "Forward", .onclick = action_go_forward);
 
 		ui_path_textfield(obj, .fill = UI_ON, .getpathelm = dav_get_pathelm, .onactivate = action_path_selected ,.varname = "path");
 
@@ -182,6 +182,15 @@
 }
 
 
+void action_go_back(UiEvent *event, void *data) {
+	DavBrowser *browser = event->document;
+	davbrowser_navigation_back(event->obj, browser);
+}
+
+void action_go_forward(UiEvent *event, void *data) {
+	DavBrowser *browser = event->document;
+	davbrowser_navigation_forward(event->obj, browser);
+}
 
 void action_path_selected(UiEvent *event, void *data) {
 	DavBrowser *browser = event->document;
--- a/application/window.h	Tue Jan 30 14:01:04 2024 +0100
+++ b/application/window.h	Tue Jan 30 16:01:44 2024 +0100
@@ -55,6 +55,9 @@
 
 void window_progress(MainWindow *win, int on);
 
+void action_go_back(UiEvent *event, void *data);
+void action_go_forward(UiEvent *event, void *data);
+
 void action_path_selected(UiEvent *event, void *data);
 
 void action_list_activate(UiEvent *event, void *data);

mercurial