Hide tabs by default, add code to Vimish to show/hide tabs
[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, CompassDirection.S, AttachOptions.Fill, AttachOptions.Shrink);
21
22         commandline.Hide();
23     }
24
25     public override void InitWebView(WebView wv) {
26         wv.KeyPressEvent += WebView_KeyPress;
27     }
28
29     private void Window_KeyPress(object o, KeyPressEventArgs e) {
30         switch(e.Event.Key) {
31         case Gdk.Key.o:
32             CommandStart("open ");
33             break;
34         case Gdk.Key.O:
35             CommandStart("open " + wt.WebView.MainFrame.Uri);
36             break;
37         case Gdk.Key.t:
38             CommandStart("tabopen ");
39             break;
40         case Gdk.Key.T:
41             CommandStart("tabopen " + wt.WebView.MainFrame.Uri);
42             break;
43         case Gdk.Key.colon:
44             CommandlineShow();
45             break;
46         }
47     }
48
49     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
50         Console.WriteLine(e.Event.Key);
51         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
52             switch(e.Event.Key) {
53             case Gdk.Key.n:
54                 wt.Tabs.NextPage();
55                 break;
56             case Gdk.Key.p:
57                 wt.Tabs.PrevPage();
58                 break;
59             }
60         } else {
61             switch(e.Event.Key) {
62             case Gdk.Key.j:
63                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
64                 break;
65             case Gdk.Key.k:
66                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
67                 break;
68             case Gdk.Key.l:
69                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
70                 break;
71             case Gdk.Key.h:
72                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
73                 break;
74             case Gdk.Key.r:
75                 wt.WebView.Reload();
76                 break;
77             case Gdk.Key.Escape:
78                 wt.WebView.ExecuteScript("document.activeElement.blur()");
79                 break;
80             }
81         }
82     }
83
84     public void CommandStart(string text) {
85         commandline.Text = text;
86         commandline.GrabFocus();
87         commandline.Position = text.Length;
88         commandline.Show();
89     }
90
91     public void CommandlineShow() {
92         commandline.Show();
93         commandline.GrabFocus();
94     }
95
96     public void CommandlineHide() {
97         commandline.Hide();
98         wt.WebView.GrabFocus();
99     }
100
101     private void command_Activate(object o, EventArgs e) {
102         string[] args = Regex.Split(commandline.Text, @"\s+");
103         switch(args[0]) {
104         case "close":
105             wt.CloseTab();
106             break;
107         case "open":
108             if (args.Length < 2) return;
109             wt.OpenUri(args[1]);
110             break;
111         case "tabopen":
112             if (args.Length < 2) return;
113             wt.OpenUriTab(args[1]);
114             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
115             break;
116         case "n":
117             wt.Tabs.NextPage();
118             break;
119         case "p":
120             wt.Tabs.PrevPage();
121             break;
122         case "q":
123             wt.Quit();
124             break;
125         case "set":
126             if (args.Length == 2)
127                 SetVar(args[1], "");
128             else
129                 SetVar(args[1], args[2]);
130             break;
131         }
132         CommandlineHide();
133     }
134
135     public void SetVar(string key, string val) {
136         switch(key) {
137         case "showtabs":
138             wt.Tabs.ShowTabs = true;
139             break;
140         case "hidetabs":
141             wt.Tabs.ShowTabs = false;
142             break;
143         default:
144             Error("No variable {0}", key);
145             break;
146         }
147     }
148
149     private void Error(string format, params object[] values) {
150         Console.WriteLine(format, values);
151     }
152
153     private void command_KeyPress(object o, KeyPressEventArgs e) {
154         if (e.Event.Key == Gdk.Key.Escape)
155             CommandlineHide();
156     }
157 }