Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
feat: begin work on IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Feb 4, 2024
1 parent 0fb6647 commit fb348ee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ meson
vala
blueprint-compiler
webkitgtk-6.0
json-glib-1.0
```

## 🏗️ Building
Expand Down
19 changes: 16 additions & 3 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ public class SkiffDesktop.MainWindow : He.ApplicationWindow {
private unowned Gtk.Box main_box;
private WebKit.WebView webview = new WebKit.WebView ();
private WebKit.UserScript script = new WebKit.UserScript (
"window.IsSkiffWindowsDesktop = true;",
"""
window.IsSkiffWindowsDesktop = true
window.chrome = {
webview: {
postMessage: (v) => window.webkit.messageHandlers.skiffDesktop.postMessage(v),
addEventListener: (_, listener) => window._skiffListener = listener,
removeEventListener: (_, listener) => delete window._skiffListener,
}
}
""",
WebKit.UserContentInjectedFrames.TOP_FRAME,
WebKit.UserScriptInjectionTime.START,
null,
Expand Down Expand Up @@ -98,8 +107,12 @@ public class SkiffDesktop.MainWindow : He.ApplicationWindow {
}

construct {
// var content_manager = webview.get_user_content_manager ();
// content_manager.add_script (script);
var message_handler = new MessageHandler ();

var content_manager = webview.get_user_content_manager ();
content_manager.add_script (script);
content_manager.script_message_received.connect (message_handler.on_script_message);
content_manager.register_script_message_handler ("skiffDesktop", null);

var network_session = webview.get_network_session ();
var website_data_manager = network_session.get_website_data_manager ();
Expand Down
40 changes: 40 additions & 0 deletions src/MessageHandler.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class SkiffDesktop.MessageHandler {
private class NotificationItem : Object {
public string title { get; set; }
public string body { get; set; }
public string threadID { get; set; }
public string emailID { get; set; }
}

private class NotificationData : Object {
public NotificationItem[] notificationData { get; set; }
}

private class UnreadData : Object {
public int numUnread { get; set; }
}

public void on_script_message (JSC.Value val) {
var parser = new Json.Parser ();
parser.load_from_data (val.to_string ());

var root = parser.get_root ();
var message_type = root.get_object ().get_string_member ("type");

switch (message_type) {
case "unreadMailCount":
var unread_data = Json.gobject_deserialize (typeof (UnreadData), root) as UnreadData;
assert (unread_data != null);
print ("unread count: %d\n", unread_data.numUnread);
break;
case "newMessageNotifications":
var notification_data = Json.gobject_deserialize (typeof (NotificationData), root) as NotificationData;
assert (notification_data != null);
print ("notifications: %d\n", notification_data.notificationData.length);
break;
default:
print ("Received unknown message %s\n", val.to_string ());
break;
}
}
}
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sources = [
'Config.vapi',
'Application.vala',
'MainWindow.vala',
'MessageHandler.vala',
]

dependencies = [
Expand All @@ -47,6 +48,7 @@ dependencies = [
dependency('libhelium-1'),
dependency('gmodule-2.0'),
dependency('webkitgtk-6.0'),
dependency('json-glib-1.0'),
]

executable(
Expand Down

0 comments on commit fb348ee

Please sign in to comment.