Getting useful...
[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.r:
41                         wt.WebView.Reload();
42                         break;
43                 case Gdk.Key.t:
44                         CommandStart("tabopen ");
45                         break;
46                 case Gdk.Key.BackSpace:
47                         wt.WebView.GoBack();
48                         break;
49                 case Gdk.Key.colon:
50                         CommandlineShow();
51                         break;
52                 case Gdk.Key.Escape:
53                         wt.WebView.ExecuteScript("document.activeElement.blur()");
54                         break;
55                 }
56         }
57
58         public void CommandStart(string text) {
59                 commandline.Text = text;
60                 commandline.GrabFocus();
61                 commandline.Position = text.Length;
62                 commandline.Show();
63         }
64
65         public void CommandlineShow() {
66                 commandline.Show();
67                 commandline.GrabFocus();
68         }
69
70         public void CommandlineHide() {
71                 commandline.Hide();
72                 wt.WebView.GrabFocus();
73         }
74
75         private void command_Activate(object o, EventArgs e) {
76                 string[] args = Regex.Split(commandline.Text, @"\s+");
77                 switch(args[0]) {
78                 case "open":
79                         if (args.Length < 2) return;
80                         wt.OpenUri(args[1]);
81                         break;
82                 case "q":
83                         wt.Quit();
84                         break;
85                 }
86                 CommandlineHide();
87         }
88
89         private void command_KeyPress(object o, KeyPressEventArgs e) {
90                 if (e.Event.Key == Gdk.Key.Escape)
91                         CommandlineHide();
92         }
93 }