Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 4e72fcb

Browse files
authored
Merge pull request #16 from mdh34/oop
object-oriented rewrite
2 parents 36bd30f + 591c5fe commit 4e72fcb

File tree

1 file changed

+151
-121
lines changed

1 file changed

+151
-121
lines changed

src/quickDocs.vala

Lines changed: 151 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -18,141 +18,171 @@
1818
*
1919
* Authored by: Matt Harris <[email protected]>
2020
*/
21+
2122
using Gtk;
2223
using WebKit;
2324

24-
void init_theme (){
25-
var window_settings = Gtk.Settings.get_default ();
26-
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
27-
var dark = user_settings.get_int ("dark");
28-
if (dark == 1) {
29-
window_settings.set ("gtk-application-prefer-dark-theme", true);
25+
public class App : Gtk.Application {
26+
27+
public App () {
28+
Object (
29+
application_id: "com.github.mdh34.quickdocs",
30+
flags: ApplicationFlags.FLAGS_NONE
31+
);
3032
}
31-
else {
32-
window_settings.set ("gtk-application-prefer-dark-theme", false);
33+
34+
35+
protected override void activate () {
36+
var window = new ApplicationWindow (this);
37+
window.set_default_size (1000, 700);
38+
window.set_border_width (12);
39+
window.set_position (WindowPosition.CENTER);
40+
var header = new HeaderBar ();
41+
header.set_show_close_button (true);
42+
window.set_titlebar (header);
43+
44+
var stack = new Stack ();
45+
stack.set_transition_type (StackTransitionType.SLIDE_LEFT_RIGHT);
46+
47+
window.destroy.connect (() => {
48+
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
49+
user_settings.set_string ("tab", stack.get_visible_child_name());
50+
});
51+
52+
var stack_switcher = new StackSwitcher ();
53+
stack_switcher.set_stack (stack);
54+
header.set_custom_title (stack_switcher);
55+
56+
var context = new WebContext ();
57+
var cookies = context.get_cookie_manager ();
58+
set_cookies (cookies);
59+
60+
var vala = new WebView();
61+
vala.load_uri ("https://valadoc.org");
62+
63+
var dev = new WebView.with_context (context);
64+
set_appcache(dev);
65+
dev.load_uri ("https://devdocs.io");
66+
67+
stack.add_titled (vala, "vala", "Valadoc");
68+
stack.add_titled (dev, "dev", "DevDocs");
69+
70+
var back = new Button.from_icon_name ("go-previous-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
71+
back.clicked.connect (() => {
72+
if (stack.get_visible_child_name () == "vala"){
73+
vala.go_back ();
74+
} else if (stack.get_visible_child_name () == "dev") {
75+
dev.go_back ();
76+
}
77+
});
78+
79+
var forward = new Button.from_icon_name ("go-next-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
80+
forward.clicked.connect (() => {
81+
if (stack.get_visible_child_name () == "vala"){
82+
vala.go_forward ();
83+
} else if (stack.get_visible_child_name () == "dev") {
84+
dev.go_forward ();
85+
}
86+
});
87+
88+
var theme_button = new Button.from_icon_name ("weather-few-clouds-symbolic");
89+
theme_button.clicked.connect(() => {
90+
toggle_theme (dev);
91+
});
92+
93+
header.add (back);
94+
header.add (forward);
95+
header.pack_end(theme_button);
96+
97+
window.add (stack);
98+
init_theme ();
99+
window.show_all();
100+
set_tab (stack);
101+
102+
var tab_switch = new SimpleAction ("switch", null);
103+
add_action (tab_switch);
104+
add_accelerator ("<Control>Tab", "app.switch", null);
105+
106+
tab_switch.activate.connect (() => {
107+
change_tab (stack);
108+
});
33109
}
34-
}
35110

36-
void set_appcache (WebView view){
37-
var host = "elementary.io";
38-
var settings = view.get_settings ();
39-
try {
40-
var resolve = Resolver.get_default ();
41-
resolve.lookup_by_name (host, null);
42-
settings.enable_offline_web_application_cache = false;
43-
} catch (Error e) {
44-
print ("Using application cache");
111+
private void init_theme () {
112+
var window_settings = Gtk.Settings.get_default ();
113+
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
114+
var dark = user_settings.get_int ("dark");
115+
116+
if (dark == 1) {
117+
window_settings.set ("gtk-application-prefer-dark-theme", true);
118+
}
119+
else {
120+
window_settings.set ("gtk-application-prefer-dark-theme", false);
121+
}
45122
}
46-
}
47123

48-
void set_cookies (CookieManager cookies){
49-
var path = (Environment.get_home_dir () + "/.config/com.github.mdh34.quickdocs/cookies");
50-
var folder = (Environment.get_home_dir () + "/.config/com.github.mdh34.quickdocs/");
51-
var file = File.new_for_path (folder);
52-
if (!file.query_exists ()){
53-
try{
54-
file.make_directory ();
55-
} catch (Error e){
56-
print ("Unable to create config directory");
57-
return;
124+
private void change_tab (Stack stack) {
125+
var current = stack.get_visible_child_name ();
126+
if (current == "vala") {
127+
stack.set_visible_child_name ("dev");
128+
} else {
129+
stack.set_visible_child_name ("vala");
58130
}
59131
}
60-
cookies.set_accept_policy (CookieAcceptPolicy.ALWAYS);
61-
cookies.set_persistent_storage (path, CookiePersistentStorage.SQLITE);
62-
}
63132

64-
void set_tab (Stack stack){
65-
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
66-
var tab = user_settings.get_string ("tab");
67-
stack.set_visible_child_name (tab);
68-
}
133+
private void set_appcache (WebView view) {
134+
var host = "elementary.io";
135+
var settings = view.get_settings ();
136+
try {
137+
var resolve = Resolver.get_default ();
138+
resolve.lookup_by_name (host, null);
139+
settings.enable_offline_web_application_cache = false;
140+
} catch (Error e) {
141+
print("Using offline mode");
142+
}
143+
}
69144

70-
void toggle_theme (WebView view){
71-
var window_settings = Gtk.Settings.get_default ();
72-
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
73-
var dark = user_settings.get_int ("dark");
74-
if (dark == 1) {
75-
window_settings.set ("gtk-application-prefer-dark-theme", false);
76-
user_settings.set_int ("dark", 0);
77-
view.run_javascript ("document.cookie = 'dark=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';", null);
78-
view.reload_bypass_cache ();
145+
private void set_cookies (CookieManager cookies) {
146+
var path = (Environment.get_home_dir () + "/.config/com.github.mdh34.quickdocs/cookies");
147+
var folder = (Environment.get_home_dir () + "/.config/com.github.mdh34.quickdocs/");
148+
var file = File.new_for_path (folder);
149+
if (!file.query_exists ()) {
150+
try {
151+
file.make_directory ();
152+
} catch (Error e) {
153+
print("Unable to create config directory");
154+
return;
155+
}
156+
}
157+
cookies.set_accept_policy (CookieAcceptPolicy.ALWAYS);
158+
cookies.set_persistent_storage (path, CookiePersistentStorage.SQLITE);
79159
}
80-
else {
81-
window_settings.set ("gtk-application-prefer-dark-theme", true);
82-
user_settings.set_int ("dark", 1);
83-
view.run_javascript ("document.cookie = 'dark=1; expires=01 Jan 2020 00:00:00 UTC';", null);
84-
view.reload_bypass_cache ();
160+
161+
private void set_tab (Stack stack) {
162+
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
163+
var tab = user_settings.get_string ("tab");
164+
stack.set_visible_child_name (tab);
85165
}
86-
}
87166

88-
int main(string[] args) {
89-
Gtk.init (ref args);
90-
var x = 1000;
91-
var y = 700;
92-
var window = new Gtk.Window ();
93-
var header = new HeaderBar ();
94-
header.set_show_close_button (true);
95-
window.set_titlebar (header);
96-
window.set_border_width (12);
97-
window.set_position (WindowPosition.CENTER);
98-
window.set_default_size (x, y);
99-
var stack = new Stack ();
100-
101-
window.destroy.connect (() => {
102-
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
103-
user_settings.set_string ("tab", stack.get_visible_child_name());
104-
Gtk.main_quit ();
105-
});
106-
107-
stack.set_transition_type (StackTransitionType.SLIDE_LEFT_RIGHT);
108-
109-
var stack_switcher = new StackSwitcher ();
110-
stack_switcher.set_stack (stack);
111-
header.set_custom_title (stack_switcher);
112-
113-
var context = new WebContext ();
114-
var cookies = context.get_cookie_manager ();
115-
set_cookies (cookies);
116-
117-
var vala = new WebView ();
118-
vala.load_uri ("https://valadoc.org/");;
119-
stack.add_titled (vala, "vala", "Valadoc");
120-
121-
var dev = new WebView.with_context (context);
122-
set_appcache(dev);
123-
dev.load_uri ("http://devdocs.io/");
124-
stack.add_titled (dev, "dev", "DevDocs");
125-
126-
var back = new Button.from_icon_name ("go-previous-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
127-
back.clicked.connect (() => {
128-
if (stack.get_visible_child_name () == "vala"){
129-
vala.go_back ();
130-
} else if (stack.get_visible_child_name () == "dev") {
131-
dev.go_back ();
167+
private void toggle_theme (WebView view) {
168+
var window_settings = Gtk.Settings.get_default ();
169+
var user_settings = new GLib.Settings ("com.github.mdh34.quickdocs");
170+
var dark = user_settings.get_int ("dark");
171+
if (dark == 1) {
172+
window_settings.set ("gtk-application-prefer-dark-theme", false);
173+
user_settings.set_int ("dark", 0);
174+
view.run_javascript ("document.cookie = 'dark=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';", null);
175+
view.reload_bypass_cache ();
176+
} else {
177+
window_settings.set ("gtk-application-prefer-dark-theme", true);
178+
user_settings.set_int ("dark", 1);
179+
view.run_javascript ("document.cookie = 'dark=1; expires=01 Jan 2020 00:00:00 UTC';", null);
180+
view.reload_bypass_cache ();
132181
}
133-
});
134-
header.add (back);
135-
136-
var forward = new Button.from_icon_name ("go-next-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
137-
forward.clicked.connect (() => {
138-
if (stack.get_visible_child_name () == "vala"){
139-
vala.go_forward ();
140-
} else if (stack.get_visible_child_name () == "dev") {
141-
dev.go_forward ();
142-
}
143-
});
144-
header.add (forward);
145-
146-
var theme_button = new Button.from_icon_name ("weather-few-clouds-symbolic");
147-
theme_button.clicked.connect(() => {
148-
toggle_theme (dev);
149-
});
150-
header.pack_end(theme_button);
151-
152-
window.add (stack);
153-
init_theme();
154-
window.show_all ();
155-
set_tab (stack);
156-
Gtk.main ();
157-
return 0;
182+
}
183+
184+
public static int main (string[] args) {
185+
var app = new App();
186+
return app.run (args);
187+
}
158188
}

0 commit comments

Comments
 (0)