Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions SlackUI/Core/ChromiumNotification.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SlackUI.Core {
class ChromiumNotification {
private const string GroupMessageTitle = "New message in #";
private const string MemberMessageTitle = "New message from ";

public void ShowNotification(String title, String body, String icon, String tag) {
Program.WrapperForm.InvokeOnUiThreadIfRequired(() => {
Toaster.Toast.ShowNotification(title, body, icon, tag);
});
var notification = Toaster.Toast.ShowNotification(title, body, icon, tag);
notification.FormClosed += delegate {
if (!notification.Dismissed)
{
var target = "";
if (notification.Title.Contains(GroupMessageTitle))
{
target = Program.ActiveTeamAddress + "/messages/" + notification.Title.Replace(GroupMessageTitle, "");
}
if (notification.Title.Contains(MemberMessageTitle))
{
target = Program.ActiveTeamAddress + "/messages/@" + notification.Title.Replace(MemberMessageTitle, "");
}

System.Diagnostics.Debug.WriteLine("[Notification] title: '{0}', body: '{1}', icon: '{2}', tag: '{3}'.", title, body, icon, tag);
if (target != "" && !Program.WrapperForm.address().Contains(target)) { Program.WrapperForm.redirect(target); }
NotificationContext.DisplayWrapperForm();
}
};
});
}
}
}
42 changes: 42 additions & 0 deletions SlackUI/Core/GeneralNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SlackUI.Core {
class GeneralNotification
{
public string permission;

public void onclick()
{
System.Diagnostics.Debug.WriteLine("[GeneralNotification/onclick]");
}

public void onshow()
{
System.Diagnostics.Debug.WriteLine("[GeneralNotification/onshow]");
}

public void onerror()
{
System.Diagnostics.Debug.WriteLine("[GeneralNotification/onerror]");
}

public void onclose()
{
System.Diagnostics.Debug.WriteLine("[GeneralNotification/onclose]");
}

public void requestPermission(String callback)
{
this.permission = "granted";
System.Diagnostics.Debug.WriteLine("[GeneralNotification/requestPermission]");
}
public void close()
{
System.Diagnostics.Debug.WriteLine("[GeneralNotification/close]");
}
}
}
11 changes: 6 additions & 5 deletions SlackUI/Core/NotificationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ internal NotificationContext() {
}
}

#endregion

#region Private Methods

/*
* Displays the wrapper form to the user.
*/
private static void DisplayWrapperForm() {
internal static void DisplayWrapperForm() {
// Focus the wrapper form or display it to the user
if(Program.WrapperForm.Visible) {
// Restore normal window state if form is minimized
Expand All @@ -99,6 +95,7 @@ private static void DisplayWrapperForm() {
using(TeamPickerForm teamPickerForm = new TeamPickerForm()) {
if(teamPickerForm.ShowDialog() == DialogResult.OK) {
Program.Settings.Data.InitialTeamToLoad = teamPickerForm.SlackTeamDomain;
Program.WrapperForm.redirect(Program.ActiveTeamAddress);
Program.WrapperForm.Show();
}
}
Expand All @@ -110,6 +107,10 @@ private static void DisplayWrapperForm() {
}
}

#endregion

#region Private Methods

/*
* Context menu open about form item click event handler.
*/
Expand Down
32 changes: 24 additions & 8 deletions SlackUI/Forms/WrapperForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public WrapperForm() {
RequestHandler = new BrowserRequestHandler()
};
chromium.RegisterJsObject("chromiumNotification", new Core.ChromiumNotification());
chromium.RegisterJsObject("Notification", new Core.GeneralNotification());

// Subscribe to multiple chromium web browser events
chromium.FrameLoadEnd += chromium_FrameLoadEnd;
Expand All @@ -59,7 +60,6 @@ public WrapperForm() {
// Save the current window state as the previous one
previousWindowState = WindowState;
}

#endregion

#region Private Methods
Expand All @@ -72,14 +72,14 @@ private void chromium_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs
if(!e.Url.Contains(AboutBlankPage)) {
// Remove the browser load overlay from the form
this.InvokeOnUiThreadIfRequired(() => {
if(browserPanel.Controls["browserLoadOverlay"].Visible) {
browserPanel.Controls["browserLoadOverlay"].Visible = false;
chromium.ExecuteScriptAsync(@"(function () {window.Notification = function (title, options) { window.chromiumNotification.showNotification(title, options.body, options.icon, options.tag); }; window.Notification.prototype = window.Notification; window.Notification.permission = 'granted'; window.Notification.requestPermission = function (callback) { window.Notification.permission = 'granted'; if(callback) { callback('granted'); } }})();");
}
browserPanel.Controls["browserLoadOverlay"].Visible = false;
chromium.ExecuteScriptAsync(@"(function () {
window.Notification = function (title, options) {
window.chromiumNotification.showNotification(title, options.body, options.icon, options.tag);
};
window.Notification.permission = 'granted';
})();");
});

// Unsubscribe the frame load end event
chromium.FrameLoadEnd -= chromium_FrameLoadEnd;
}
}

Expand Down Expand Up @@ -210,6 +210,22 @@ protected override void WndProc(ref Message m) {

#endregion


#region Internal Methods

public void redirect(String url)
{
browserPanel.Controls["browserLoadOverlay"].Visible = true;
chromium.NavStateChanged += chromium_NavStateChanged;
chromium.Load(url);
}

public string address()
{
return chromium.Address;
}

#endregion
}

}
1 change: 1 addition & 0 deletions SlackUI/SlackUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Core\ChromiumNotification.cs" />
<Compile Include="Core\GeneralNotification.cs" />
<Compile Include="Extensions\ControlExtension.cs" />
<Compile Include="Handlers\BrowserLifeSpanHandler.cs" />
<Compile Include="Forms\BaseForm.cs" />
Expand Down