Skip to content

Commit

Permalink
Updates on Notification System (fixes #7 )
Browse files Browse the repository at this point in the history
Mac: Change the notification system to Apple's native notification system.
Win: code impovements
  • Loading branch information
dedekind125 committed May 20, 2019
1 parent 48e92ba commit 02f0c32
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
32 changes: 19 additions & 13 deletions ChessClaimView.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
from PyQt5.QtCore import Qt, QSize
from helpers import resource_path

# Notification Import
if (platform.system() == "Darwin"):
import pync
elif(platform.system() == "Windows"):
from win10toast import ToastNotifier

class ChessClaimView(QMainWindow):
""" The main window of the application.
Attributes:
rowCount(int): The number of the row the TreeView Table has.
iconsSize(int): The recommended size of the icons.
mac_notification: Notification for macOS
win_notification: Notification for windows OS
"""
def __init__(self):
super().__init__()
Expand All @@ -46,6 +42,13 @@ def __init__(self):

self.rowCount = 0

if (platform.system() == "Darwin"):
from MacNotification import Notification
self.mac_notification = Notification()
elif (platform.system() == "Windows"):
from win10toast import ToastNotifier
self.win_notification = ToastNotifier()

def center(self):
""" Centers the window on the screen """
screen = QDesktopWidget().screenGeometry()
Expand Down Expand Up @@ -201,15 +204,18 @@ def add_to_table(self,type,bo_number,players,move):
self.notify(type,players,move)

def notify(self,type,players,move):
""" Send notification depending on the OS.
Args:
type: The type of the draw (3 Fold Repetition, 5 Fold Repetition,
50 Moves Rule, 75 Moves Rule).
players: The names of the players.
move: With which move the draw is valid.
"""
if (platform.system() == "Darwin"):
pync.notify(title=type,
subtitle=players,
message=move,
appIcon=resource_path("logo.png"),
sender="com.brainfriz.chess-claim-tool")
self.mac_notification.clearNotifications()
self.mac_notification.notify(type,players,move)
elif(platform.system() == "Windows"):
toaster = ToastNotifier()
toaster.show_toast(type,
self.win_notification.show_toast(type,
players+"\n"+move,
icon_path=resource_path("logo.ico"),
duration=5,
Expand Down
29 changes: 29 additions & 0 deletions MacNotification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from AppKit import NSImage
from Foundation import NSUserNotification, NSUserNotificationCenter

class Notification():
""" The Notification System using Apple's native Notifications System
Attributes:
notification: The notification
center: Apple notification center.
"""
def __init__(self):
self.notification = NSUserNotification.alloc().init()
self.center = NSUserNotificationCenter.defaultUserNotificationCenter()
self.center.setDelegate_(self)

def userNotificationCenter_shouldPresentNotification_(self, center, notification):
return True

def clearNotifications(self):
"""Clear any displayed alerts we have posted."""
self.center.removeAllDeliveredNotifications()

def notify(self,title,subtitle,text):
"Set notification details and send the notification"
self.notification.setTitle_(title)
self.notification.setSubtitle_(subtitle)
self.notification.setInformativeText_(text)
self.notification.setSoundName_("NSUserNotificationDefaultSoundName")

self.center.scheduleNotification_(self.notification)

0 comments on commit 02f0c32

Please sign in to comment.