Finally, a version I'm proud of. :)
[WebThing.git] / plugins / Vimish.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using Gtk;
4 using bytex64.WebThing;
5
6 public class Plugin {
7         WebThing wt;
8         Gtk.Entry commandline;
9
10         public Plugin(WebThing wt) {
11                 this.wt = wt;
12                 wt.WebView.KeyPressEvent += WebView_KeyPress;
13
14                 commandline = new Gtk.Entry();
15                 commandline.Activated += command_Activate;
16                 commandline.KeyPressEvent += command_KeyPress;
17                 wt.AttachWidget(commandline, CompassDirection.S, AttachOptions.Fill, AttachOptions.Shrink);
18
19                 commandline.Hide();
20         }
21
22         private void WebView_KeyPress(object o, KeyPressEventArgs e) {
23                 Console.WriteLine(e.Event.Key);
24                 switch(e.Event.Key) {
25                 case Gdk.Key.j:
26                         wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
27                         break;
28                 case Gdk.Key.k:
29                         wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
30                         break;
31                 case Gdk.Key.l:
32                         wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
33                         break;
34                 case Gdk.Key.h:
35                         wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
36                         break;
37                 case Gdk.Key.o:
38                         CommandStart("open ");
39                         break;
40                 case Gdk.Key.t:
41                         CommandStart("tabopen ");
42                         break;
43                 case Gdk.Key.BackSpace:
44                         wt.WebView.GoBack();
45                         break;
46                 case Gdk.Key.colon:
47                         CommandlineShow();
48                         break;
49                 }
50         }
51
52         public void CommandStart(string text) {
53                 commandline.Text = text;
54                 commandline.GrabFocus();
55                 commandline.Position = text.Length;
56                 commandline.Show();
57         }
58
59         public void CommandlineShow() {
60                 commandline.Show();
61                 commandline.GrabFocus();
62         }
63
64         public void CommandlineHide() {
65                 commandline.Hide();
66                 wt.WebView.GrabFocus();
67         }
68
69         private void command_Activate(object o, EventArgs e) {
70                 string[] args = Regex.Split(commandline.Text, @"\s+");
71                 switch(args[0]) {
72                 case "open":
73                         if (args.Length < 2) return;
74                         wt.WebView.Open(args[1]);
75                         break;
76                 }
77                 commandline.Text = "";
78                 CommandlineHide();
79         }
80
81         private void command_KeyPress(object o, KeyPressEventArgs e) {
82                 if (e.Event.Key == Gdk.Key.Escape)
83                         CommandlineHide();
84         }
85 }