2019-09-26 19:39:29 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Display and run a Pomodoro timer.
|
|
|
|
Left click to start timer, left click again to pause.
|
|
|
|
Right click will cancel the timer.
|
|
|
|
"""
|
|
|
|
|
2019-09-27 19:33:59 +02:00
|
|
|
from __future__ import absolute_import
|
2019-09-26 19:39:29 +02:00
|
|
|
import datetime
|
2019-09-29 01:27:26 +02:00
|
|
|
from math import ceil
|
2019-09-26 19:39:29 +02:00
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
widgets = bumblebee.output.Widget(full_text=self.text)
|
2019-09-29 22:57:46 +02:00
|
|
|
self.work_period = 25
|
|
|
|
self.break_period = 5
|
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.work_period)
|
2019-09-29 01:27:26 +02:00
|
|
|
self.remaining_time_str = "{}m ".format(ceil((self.remaining_time.seconds / 60)))
|
2019-09-29 22:57:46 +02:00
|
|
|
# self.remaining_time_str = "{}min{}s ".format(int((self.remaining_time.seconds / 60)),
|
|
|
|
# round((self.remaining_time.seconds/60) % 1*60))
|
2019-09-26 19:39:29 +02:00
|
|
|
self.time = None
|
2019-09-29 01:27:26 +02:00
|
|
|
self.pomodoro = { "state":"OFF", "type": ""}
|
|
|
|
self._text = self.remaining_time_str + self.pomodoro["type"]
|
2019-09-26 19:39:29 +02:00
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd=self.timer_play_pause)
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE,
|
|
|
|
cmd=self.timer_reset)
|
|
|
|
|
|
|
|
def text(self, widget):
|
|
|
|
return "{}".format(self._text)
|
|
|
|
|
|
|
|
def update(self, widget):
|
|
|
|
if self.pomodoro["state"] == "ON":
|
|
|
|
timediff = (datetime.datetime.now() - self.time)
|
|
|
|
if timediff.seconds >= 0:
|
|
|
|
self.remaining_time -= timediff
|
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
|
|
|
|
if self.remaining_time.seconds <= 0:
|
2019-09-29 01:27:26 +02:00
|
|
|
if self.pomodoro["type"] == "Work":
|
|
|
|
self.pomodoro["type"] = "Break"
|
2019-09-29 22:57:46 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.break_period)
|
2019-09-29 01:27:26 +02:00
|
|
|
elif self.pomodoro["type"] == "Break":
|
|
|
|
self.pomodoro["type"] = "Work"
|
2019-09-29 22:57:46 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.work_period)
|
2019-09-26 19:39:29 +02:00
|
|
|
|
2019-09-29 01:27:26 +02:00
|
|
|
self.remaining_time_str = "{}m ".format(ceil((self.remaining_time.seconds / 60)))
|
2019-09-29 22:57:46 +02:00
|
|
|
# self.remaining_time_str = "{}min{}s ".format(int((self.remaining_time.seconds / 60)),
|
|
|
|
# round((self.remaining_time.seconds / 60) % 1 * 60))
|
2019-09-29 01:27:26 +02:00
|
|
|
self._text = self.remaining_time_str + self.pomodoro["type"]
|
2019-09-26 19:39:29 +02:00
|
|
|
|
|
|
|
def timer_play_pause(self, widget):
|
|
|
|
if self.pomodoro["state"] == "OFF":
|
2019-09-29 01:27:26 +02:00
|
|
|
self.pomodoro = {"state": "ON", "type": "Work"}
|
2019-09-29 22:57:46 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.work_period)
|
2019-09-26 19:39:29 +02:00
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
elif self.pomodoro["state"] == "ON":
|
|
|
|
self.pomodoro["state"] = "PAUSED"
|
|
|
|
self.remaining_time -= (datetime.datetime.now() - self.time)
|
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
elif self.pomodoro["state"] == "PAUSED":
|
|
|
|
self.pomodoro["state"] = "ON"
|
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
|
|
|
|
def timer_reset(self, widget):
|
|
|
|
if self.pomodoro["state"] == "ON" or self.pomodoro["state"] == "PAUSED":
|
2019-09-29 01:27:26 +02:00
|
|
|
self.pomodoro = {"state":"OFF", "type": "" }
|
2019-09-29 22:57:46 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.work_period)
|
2019-09-29 01:27:26 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = [];
|
|
|
|
state.append(self.pomodoro["state"].lower())
|
|
|
|
if self.pomodoro["state"] == "ON" or self.pomodoro["state"] == "OFF":
|
|
|
|
state.append(self.pomodoro["type"].lower())
|
|
|
|
|
|
|
|
return state
|