Fix: Focus startup WebView
[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         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
33             switch(e.Event.Key) {
34             case Gdk.Key.n:
35                 wt.Tabs.NextPage();
36                 break;
37             case Gdk.Key.p:
38                 wt.Tabs.PrevPage();
39                 break;
40             }
41         } else {
42             switch(e.Event.Key) {
43             case Gdk.Key.o:
44                 CommandStart("open ");
45                 break;
46             case Gdk.Key.O:
47                 CommandStart("open " + wt.WebView.MainFrame.Uri);
48                 break;
49             case Gdk.Key.t:
50                 CommandStart("tabopen ");
51                 break;
52             case Gdk.Key.T:
53                 CommandStart("tabopen " + wt.WebView.MainFrame.Uri);
54                 break;
55             case Gdk.Key.colon:
56                 CommandlineShow();
57                 break;
58             }
59         }
60     }
61
62     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
63         WebView wv = (WebView)o;
64
65         Console.WriteLine(e.Event.Key);
66         if (e.Event.State == Gdk.ModifierType.None) {
67             switch(e.Event.Key) {
68             case Gdk.Key.j:
69                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
70                 break;
71             case Gdk.Key.k:
72                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
73                 break;
74             case Gdk.Key.l:
75                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
76                 break;
77             case Gdk.Key.h:
78                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
79                 break;
80             case Gdk.Key.r:
81                 wv.Reload();
82                 break;
83             case Gdk.Key.Escape:
84                 wv.ExecuteScript("document.activeElement.blur()");
85                 break;
86             }
87         }
88     }
89
90     public void CommandStart(string text) {
91         commandline.Text = text;
92         commandline.GrabFocus();
93         commandline.Position = text.Length;
94         commandline.Show();
95     }
96
97     public void CommandlineShow() {
98         commandline.Show();
99         commandline.GrabFocus();
100     }
101
102     public void CommandlineHide() {
103         commandline.Hide();
104         wt.WebView.GrabFocus();
105     }
106
107     private void command_Activate(object o, EventArgs e) {
108         string[] args = Regex.Split(commandline.Text, @"\s+");
109         switch(args[0]) {
110         case "close":
111             wt.CloseTab();
112             break;
113         case "open":
114             if (args.Length < 2) return;
115             wt.OpenUri(args[1]);
116             break;
117         case "tabopen":
118             if (args.Length < 2) return;
119             wt.OpenUriTab(args[1]);
120             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
121             break;
122         case "n":
123             wt.Tabs.NextPage();
124             break;
125         case "p":
126             wt.Tabs.PrevPage();
127             break;
128         case "q":
129             wt.Quit();
130             break;
131         case "set":
132             if (args.Length == 2)
133                 Options[args[1]] = null;
134             else
135                 Options[args[1]] = args[2];
136             ApplyOptions();
137             break;
138         }
139         CommandlineHide();
140     }
141
142     private void ApplyOptions() {
143         foreach (string key in Options.Keys) {
144             switch(key) {
145             case "ShowTabs":
146                 bool v = ParseBool(Options[key]);
147                 wt.Tabs.ShowTabs = v;
148                 break;
149             default:
150                 Error("No variable {0}", key);
151                 break;
152             }
153         }
154     }
155
156     private bool ParseBool(string v) {
157         switch(v.ToLower()) {
158         case "true":
159         case "t":
160         case "1":
161         case "on":
162         case "yes":
163             return true;
164         }
165         return false;
166     }
167
168     private void Error(string format, params object[] values) {
169         Console.WriteLine(format, values);
170     }
171
172     private void command_KeyPress(object o, KeyPressEventArgs e) {
173         if (e.Event.Key == Gdk.Key.Escape)
174             CommandlineHide();
175     }
176 }