From 7bf09cef03c7fd78e599039b14a29e8a3f3eed42 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Tue, 2 Jun 2009 02:58:48 -0500 Subject: [PATCH] Getting useful... Added Firefox-style keyboard navigation shortcuts and command-line handling --- WebThing | 2 +- WebThing.cs | 99 ++++++++++++++++++++++++++++++++++-------- plugins.conf | 1 + plugins/DefaultPage.cs | 6 ++- plugins/FFNav.cs | 31 +++++++++++++ plugins/Makefile | 2 +- plugins/Vimish.cs | 12 ++++- 7 files changed, 131 insertions(+), 22 deletions(-) create mode 100644 plugins/FFNav.cs diff --git a/WebThing b/WebThing index 7d14c4b..50e56f3 100755 --- a/WebThing +++ b/WebThing @@ -4,4 +4,4 @@ export LD_LIBRARY_PATH=plugins:$LD_LIBRARY_PATH echo $LD_LIBRARY_PATH -exec mono main.exe +exec mono main.exe "$@" diff --git a/WebThing.cs b/WebThing.cs index 04ede67..f36a8fa 100644 --- a/WebThing.cs +++ b/WebThing.cs @@ -2,14 +2,14 @@ using System; using System.Collections.Generic; using System.Reflection; using System.IO; +using System.Text.RegularExpressions; using Gtk; using GtkSharp; using WebKit; namespace bytex64.WebThing { public enum CompassDirection { - N, NE, E, SE, S, SW, W, NW, - Interior + N, E, S, W, Interior } public class WebThing { @@ -44,11 +44,16 @@ namespace bytex64.WebThing { private Gtk.Alignment InteriorOverlay; public Dictionary Plugins; + public Dictionary Options; + public string[] Arguments; public void Run() { Application.Init(); + + ParseArgs(); + _Window = new Gtk.Window("WebThing"); - _Window.Destroyed += delegate { Application.Quit(); }; + _Window.Destroyed += delegate { Quit(); }; WidgetGrid = new Gtk.Table(3, 3, false); _Window.Add(WidgetGrid); @@ -80,6 +85,66 @@ namespace bytex64.WebThing { Application.Run(); } + protected void ParseArgs() { + Options = new Dictionary(); + Queue q = new Queue(); + foreach (string s in System.Environment.GetCommandLineArgs()) { + q.Enqueue(s); + } + q.Dequeue(); // Remove self argument + + Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$"); + Regex LongOpt = new Regex(@"^--([\w-]+)$"); + string OptLast = null; + List args = new List(); + + while (q.Count > 0) { + string opt = q.Dequeue(); + Match m; + + m = SimpleOpt.Match(opt); + if (m.Success) { + string s = m.Groups[1].Value; + if (s.Length > 1) { + foreach (char c in s) { + Options[c.ToString()] = ""; + } + OptLast = null; + } else { + Options[s] = ""; + OptLast = s; + } + continue; + } + + m = LongOpt.Match(opt); + if (m.Success) { + string s = m.Groups[1].Value; + Options[s] = ""; + OptLast = s; + continue; + } + + // else + + if (OptLast != null) { + Options[OptLast] = opt; + OptLast = null; + } else { + args.Add(opt); + } + } + Arguments = args.ToArray(); + + /* + Console.WriteLine("Options:"); + foreach (string key in Options.Keys) + Console.WriteLine(" {0}\t{1}", key, Options[key]); + + Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments)); + */ + } + public void LoadPlugin(string assemblyname) { Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll"); object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null); @@ -89,29 +154,17 @@ namespace bytex64.WebThing { public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) { switch(direction) { case CompassDirection.N: - WidgetGrid.Attach(widget, 1, 2, 0, 1, xoptions, yoptions, 0, 0); - break; - case CompassDirection.NE: - WidgetGrid.Attach(widget, 2, 3, 0, 1, xoptions, yoptions, 0, 0); + WidgetGrid.Attach(widget, 0, 3, 0, 1, xoptions, yoptions, 0, 0); break; case CompassDirection.E: WidgetGrid.Attach(widget, 2, 3, 1, 2, xoptions, yoptions, 0, 0); break; - case CompassDirection.SE: - WidgetGrid.Attach(widget, 2, 3, 2, 3, xoptions, yoptions, 0, 0); - break; case CompassDirection.S: - WidgetGrid.Attach(widget, 1, 2, 2, 3, xoptions, yoptions, 0, 0); - break; - case CompassDirection.SW: - WidgetGrid.Attach(widget, 0, 1, 2, 3, xoptions, yoptions, 0, 0); + WidgetGrid.Attach(widget, 0, 3, 2, 3, xoptions, yoptions, 0, 0); break; case CompassDirection.W: WidgetGrid.Attach(widget, 0, 1, 1, 2, xoptions, yoptions, 0, 0); break; - case CompassDirection.NW: - WidgetGrid.Attach(widget, 0, 1, 0, 1, xoptions, yoptions, 0, 0); - break; case CompassDirection.Interior: InteriorOverlay.Add(widget); break; @@ -121,5 +174,17 @@ namespace bytex64.WebThing { public void AttachWidget(Gtk.Widget widget, CompassDirection direction) { AttachWidget(widget, direction, 0, 0); } + + public void OpenUri(string Uri) { + if (!Regex.IsMatch(Uri, @"://")) { + Uri = String.Format("http://{0}", Uri); + } + wv.Open(Uri); + } + + public void Quit() { + // TODO: Create a way of shutting down plugins + Application.Quit(); + } } } diff --git a/plugins.conf b/plugins.conf index 72eb6e4..271eca5 100644 --- a/plugins.conf +++ b/plugins.conf @@ -1,4 +1,5 @@ SoupSettings LoadProgress Vimish +FFNav DefaultPage diff --git a/plugins/DefaultPage.cs b/plugins/DefaultPage.cs index e1a0010..9d8913d 100644 --- a/plugins/DefaultPage.cs +++ b/plugins/DefaultPage.cs @@ -3,6 +3,10 @@ using bytex64.WebThing; public class Plugin { public Plugin(WebThing wt) { - wt.WebView.Open("http://dominionofawesome.com/"); + if (wt.Arguments.Length > 0) { + wt.OpenUri(wt.Arguments[0]); + } else { + wt.OpenUri("http://dominionofawesome.com/"); + } } } diff --git a/plugins/FFNav.cs b/plugins/FFNav.cs new file mode 100644 index 0000000..1a3145c --- /dev/null +++ b/plugins/FFNav.cs @@ -0,0 +1,31 @@ +using System; +using Gtk; +using bytex64.WebThing; + +public class Plugin { + WebThing wt; + + public Plugin(WebThing wt) { + this.wt = wt; + wt.WebView.KeyPressEvent += WebView_KeyPress; + } + + private void WebView_KeyPress(object o, KeyPressEventArgs e) { + if ((e.Event.State & Gdk.ModifierType.Mod1Mask) != 0) { + switch(e.Event.Key) { + case Gdk.Key.Left: + wt.WebView.GoBack(); + break; + case Gdk.Key.Right: + wt.WebView.GoForward(); + break; + } + } else { + switch(e.Event.Key) { + case Gdk.Key.BackSpace: + wt.WebView.GoBack(); + break; + } + } + } +} diff --git a/plugins/Makefile b/plugins/Makefile index d742115..1583fde 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -1,7 +1,7 @@ CSFLAGS = -debug references = -r:../webkit-sharp.dll -pkg:gtk-sharp-2.0 -all: Vimish.dll DefaultPage.dll SoupSettings.dll LoadProgress.dll +all: Vimish.dll FFNav.dll DefaultPage.dll SoupSettings.dll LoadProgress.dll clean: rm -f *.dll *.mdb *.so diff --git a/plugins/Vimish.cs b/plugins/Vimish.cs index bb56ed0..ad2a9e5 100644 --- a/plugins/Vimish.cs +++ b/plugins/Vimish.cs @@ -37,6 +37,9 @@ public class Plugin { case Gdk.Key.o: CommandStart("open "); break; + case Gdk.Key.r: + wt.WebView.Reload(); + break; case Gdk.Key.t: CommandStart("tabopen "); break; @@ -46,6 +49,9 @@ public class Plugin { case Gdk.Key.colon: CommandlineShow(); break; + case Gdk.Key.Escape: + wt.WebView.ExecuteScript("document.activeElement.blur()"); + break; } } @@ -71,10 +77,12 @@ public class Plugin { switch(args[0]) { case "open": if (args.Length < 2) return; - wt.WebView.Open(args[1]); + wt.OpenUri(args[1]); + break; + case "q": + wt.Quit(); break; } - commandline.Text = ""; CommandlineHide(); } -- 2.25.1