Skip to content

Commit

Permalink
Add WebView Sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Bleak Grey committed May 8, 2020
1 parent 2023ffe commit 951ec9a
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 40 deletions.
6 changes: 3 additions & 3 deletions data/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</child>
<child>
<object class="GtkBox">
<property name="width_request">600</property>
<property name="height_request">400</property>
<property name="width_request">800</property>
<property name="height_request">500</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
Expand All @@ -36,7 +36,7 @@
<object class="HdyViewSwitcherBar" id="switcher">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stack">stateful</property>
<property name="policy">auto</property>
<property name="reveal">True</property>
</object>
<packing>
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ sources = [
'src/Widgets/WebView.vala',
'src/Selection.vala',
'src/Source.vala',
'src/WebViewSource.vala',
'src/Sources/Wikitionary.vala',
'src/Sources/Wordnik.vala',
]

subdir('data')
Expand Down
15 changes: 11 additions & 4 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ namespace HeadsUp {
add_window (window);
}
window.present ();

Source.register (new Sources.Wikitionary ());
register_sources ();

var query = Selection.grab ();
query = "heads-up";
if (query != "")

if (Source.registry.size < 1)
window.no_sources ();
else if (query != "")
window.look_up (query);
else
window.empty_state ();
window.no_selection ();
}

public static int main (string[] args) {
Expand All @@ -44,4 +46,9 @@ namespace HeadsUp {
return (string) res.get_data ();
}

void register_sources () {
Source.register (new Sources.Wikitionary ());
Source.register (new Sources.Wordnik ());
}

}
30 changes: 14 additions & 16 deletions src/Source.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,30 @@ public class HeadsUp.Source : GLib.Object {
public string title { get; construct set; }
public string icon { get; construct set; }
public Widget content {get; construct set; default = null; }
public string query { get; set; }

public Source () {
protected Widgets.Stateful stateful;

}

public virtual void on_selected () {
if (content == null) {
warning (@"$title has no content widget!");
return;
}
public virtual void on_registered (Stack stack) {
stateful = new Widgets.Stateful ();

if (content == null)
warning (@"Source $title has no content widget!");
else
stateful.content.pack_start (content, true, true);

stack.add_titled (stateful, title, title);
stack.child_set_property (stateful, "icon-name", icon);
}

public virtual void on_registered (Stack stack) {
var custom_content = new Box (Orientation.VERTICAL, 0);
stack.add_titled (custom_content, title, title);
stack.child_set_property (custom_content, "icon-name", icon);
custom_content.show ();
public virtual bool on_lookup (string q) {
stateful.show_status (_("Nothing Here"), _("Definition unavailable"));
return true;
}



/* Static Registry */

protected static ArrayList<Source> registry = new ArrayList<Source> ();
public static ArrayList<Source> registry = new ArrayList<Source> ();

public static void register (Source source) {
info (@"Adding new source: $(source.title)");
Expand Down
6 changes: 5 additions & 1 deletion src/Sources/Wikitionary.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class HeadsUp.Sources.Wikitionary : Source {
public class HeadsUp.Sources.Wikitionary : WebViewSource {

public Wikitionary () {
Object (
Expand All @@ -7,4 +7,8 @@ public class HeadsUp.Sources.Wikitionary : Source {
);
}

public override string get_url () {
return @"https://$(app.window.lang).wiktionary.org/w/index.php?printable=yes&redirects=1&title=$query";
}

}
14 changes: 14 additions & 0 deletions src/Sources/Wordnik.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class HeadsUp.Sources.Wordnik : WebViewSource {

public Wordnik () {
Object (
title: "Wordnik",
icon: "document-properties-symbolic"
);
}

public override string get_url () {
return @"https://www.wordnik.com/words/$query";
}

}
28 changes: 28 additions & 0 deletions src/WebViewSource.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class HeadsUp.WebViewSource : Source {

Widgets.WebView webview;

construct {
webview = new Widgets.WebView ();
webview.on_ready.connect (() => {
stateful.show_content ();
});
content = webview;
}

public override bool on_lookup (string q) {
query = q;
reload ();
return true;
}

public virtual string get_url () {
return "";
}

void reload () {
stateful.show_loading ();
webview.load_uri (get_url ());
}

}
8 changes: 6 additions & 2 deletions src/Widgets/Stateful.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ public class HeadsUp.Widgets.Stateful : Stack {
[GtkChild]
public Box content;

public Stateful () {
public void show_loading () {
visible_child_name = "loading";
}

public void show_content () {
visible_child = content;
}

public void set_status (string label, string desc, string icon = "dialog-warning-symbolic") {
public void show_status (string label, string desc, string icon = "dialog-warning-symbolic") {
status_image.icon_name = icon;
status_label.label = @"<span size=\"xx-large\">$label</span>";
status_desc.label = desc;
Expand Down
28 changes: 14 additions & 14 deletions src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,35 @@ public class HeadsUp.Window : Gtk.Window {
lang_settings = new GLib.Settings ("org.gnome.desktop.input-sources");
lang_settings.changed["current"].connect (() => on_lang_changed ());
lang_settings.changed["mru-sources"].connect (() => on_lang_changed ());

// webview = new WebView ();
// webview.on_ready.connect (() => {
// state.visible_child = content;
// });
// content.add (webview);
}

public void empty_state () {
stateful.set_status (_("No text selected"),
public void no_selection () {
header.title = "";
header.subtitle = "";
switcher.reveal = false;
stateful.show_status (_("No Selection"),
_("Select something in any window to search for its definition"),
"edit-find-symbolic");
}

public void no_sources () {
header.title = "";
header.subtitle = "";
switcher.reveal = false;
stateful.show_status (_("No Sources"),
_("You don't have any lookup sources available"),
"edit-find-symbolic");
}

public void look_up (string q) {
query = q;
header.title = @"\"$query\"";
//header.subtitle = _("provided by Wikitionary (CC BY-SA 3.0)");
switcher.reveal = true;
stateful.show_content ();
on_lang_changed ();
}

// void reload_webview () {
// state.visible_child = loading;
// webview.load_uri (@"https://$lang.wiktionary.org/w/index.php?printable=yes&redirects=1&title=$query");
// }
Source.registry.@foreach (s => s.on_lookup (q));
}

void on_theme_changed () {
var theme = settings.gtk_theme_name;
Expand Down

0 comments on commit 951ec9a

Please sign in to comment.