Rearrange initialization to be clearer and shorter
[WebThing.git] / plugins / Vimish.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using Gtk;
4 using WebKit;
5 using bytex64.WebThing;
6
7 public class Vimish : WebThingPlugin {
8     WebThing wt;
9     Gtk.Entry commandline;
10
11     public override void Init(WebThing wt) {
12         this.wt = wt;
13         wt.Tabs.ShowTabs = false;
14
15         wt.Window.KeyPressEvent += Window_KeyPress;
16
17         commandline = new Gtk.Entry();
18         commandline.Activated += command_Activate;
19         commandline.KeyPressEvent += command_KeyPress;
20         wt.AttachWidget(commandline, AttachPoint.S, AttachOptions.Fill, AttachOptions.Shrink);
21
22         commandline.Hide();
23
24         ApplyOptions();
25     }
26
27     public override void InitWebView(WebView wv) {
28         wv.KeyPressEvent += WebView_KeyPress;
29     }
30
31     private void Window_KeyPress(object o, KeyPressEventArgs e) {
32         switch(e.Event.Key) {
33         case Gdk.Key.o:
34             CommandStart("open ");
35             break;
36         case Gdk.Key.O:
37             CommandStart("open " + wt.WebView.MainFrame.Uri);
38             break;
39         case Gdk.Key.t:
40             CommandStart("tabopen ");
41             break;
42         case Gdk.Key.T:
43             CommandStart("tabopen " + wt.WebView.MainFrame.Uri);
44             break;
45         case Gdk.Key.colon:
46             CommandlineShow();
47             break;
48         }
49     }
50
51     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
52         Console.WriteLine(e.Event.Key);
53         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
54             switch(e.Event.Key) {
55             case Gdk.Key.n:
56                 wt.Tabs.NextPage();
57                 break;
58             case Gdk.Key.p:
59                 wt.Tabs.PrevPage();
60                 break;
61             }
62         } else {
63             switch(e.Event.Key) {
64             case Gdk.Key.j:
65                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
66                 break;
67             case Gdk.Key.k:
68                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
69                 break;
70             case Gdk.Key.l:
71                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
72                 break;
73             case Gdk.Key.h:
74                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
75                 break;
76             case Gdk.Key.r:
77                 wt.WebView.Reload();
78                 break;
79             case Gdk.Key.Escape:
80                 wt.WebView.ExecuteScript("document.activeElement.blur()");
81                 break;
82             }
83         }
84     }
85
86     public void CommandStart(string text) {
87         commandline.Text = text;
88         commandline.GrabFocus();
89         commandline.Position = text.Length;
90         commandline.Show();
91     }
92
93     public void CommandlineShow() {
94         commandline.Show();
95         commandline.GrabFocus();
96     }
97
98     public void CommandlineHide() {
99         commandline.Hide();
100         wt.WebView.GrabFocus();
101     }
102
103     private void command_Activate(object o, EventArgs e) {
104         string[] args = Regex.Split(commandline.Text, @"\s+");
105         switch(args[0]) {
106         case "close":
107             wt.CloseTab();
108             break;
109         case "open":
110             if (args.Length < 2) return;
111             wt.OpenUri(args[1]);
112             break;
113         case "tabopen":
114             if (args.Length < 2) return;
115             wt.OpenUriTab(args[1]);
116             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
117             break;
118         case "n":
119             wt.Tabs.NextPage();
120             break;
121         case "p":
122             wt.Tabs.PrevPage();
123             break;
124         case "q":
125             wt.Quit();
126             break;
127         case "set":
128             if (args.Length == 2)
129                 Options[args[1]] = null;
130             else
131                 Options[args[1]] = args[2];
132             ApplyOptions();
133             break;
134         }
135         CommandlineHide();
136     }
137
138     private void ApplyOptions() {
139         foreach (string key in Options.Keys) {
140             switch(key) {
141             case "ShowTabs":
142                 bool v = ParseBool(Options[key]);
143                 wt.Tabs.ShowTabs = v;
144                 break;
145             default:
146                 Error("No variable {0}", key);
147                 break;
148             }
149         }
150     }
151
152     private bool ParseBool(string v) {
153         switch(v.ToLower()) {
154         case "true":
155         case "t":
156         case "1":
157         case "on":
158         case "yes":
159             return true;
160         }
161         return false;
162     }
163
164     private void Error(string format, params object[] values) {
165         Console.WriteLine(format, values);
166     }
167
168     private void command_KeyPress(object o, KeyPressEventArgs e) {
169         if (e.Event.Key == Gdk.Key.Escape)
170             CommandlineHide();
171     }
172 }