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