From bb6896e0bac8e94785b98470a6c75c79dcb535cb Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 7 Jul 2011 15:52:09 -0400 Subject: [PATCH] flash window according to "flash_rate" conf param when unread count > 0 --- Window.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Window.py b/Window.py index 9ce3b90..02d3b21 100755 --- a/Window.py +++ b/Window.py @@ -2,6 +2,8 @@ import gtk import gobject import imaplib +import math +from datetime import datetime, timedelta class Window(object): def __init__(self, title, conf): @@ -11,6 +13,7 @@ class Window(object): self.fgcolor = ''.join(map(lambda x: '%02x' % int(0xFF * x), self.conf['fgcolor'])) self.font_sz = 1024 * self.conf['font_size'] + self.flash_rate = 0 if not 'width' in self.conf: self.conf['width'] = 32 @@ -24,6 +27,10 @@ class Window(object): self.conf['fgcolor'] = (0.0, 0.0, 0.0) if not 'bgcolor' in self.conf: self.conf['bgcolor'] = (1.0, 0.5, 0.0) + if not 'bgcolor2' in self.conf: + self.conf['bgcolor2'] = (1.0, 1.0, 1.0) + if 'flash_rate' in self.conf: + self.flash_rate = self.conf['flash_rate'] if not 'font_size' in self.conf: self.conf['font_size'] = 18 sticky = True @@ -88,7 +95,11 @@ class Window(object): try: self.connection.select(self.conf['mailbox']) msgs = self.connection.search(None, 'UnSeen')[1][0].split() + old_count = self.count self.count = len(msgs) + if self.flash_rate > 0 and old_count == 0 and self.count > 0: + self.flash_start_dt = datetime.now() + gobject.timeout_add(100, self.do_flash) except: self.connection = None self.update_count() @@ -103,6 +114,22 @@ class Window(object): 'font_size="%d">%d' % (self.fgcolor, self.font_sz, self.count))) + def do_flash(self): + if self.count > 0: + delta = datetime.now() - self.flash_start_dt + delta_msec = delta.seconds * 1000 + delta.microseconds / 1000 + period = delta_msec / float(self.flash_rate) + mix = (math.sin(math.pi * 2 * period) + 1.0) / 2.0 + else: + mix = 0 + linear_combination = lambda (x, y): x + (y - x) * mix + bg1 = self.conf['bgcolor'] + bg2 = self.conf['bgcolor2'] + flash_bgcolor = map(linear_combination, zip(bg1, bg2)) + self.viewport.modify_bg(gtk.STATE_NORMAL, + gtk.gdk.Color(*map(lambda x: int(65535 * x), flash_bgcolor))) + return self.count > 0 + def destroy_event(self): self.disconnect() self.update_conf()