from dynmen.rofi import Rofi
from dynmen.dmenu import DMenu

from libqtile.widget import base
from taskw import TaskWarrior

class TaskWarriorWidget(base.ThreadPoolText):
    """ TaskWarrior Widget

    Widget requirements: taskw, taskwarrior, dynmen.

    This widget displays your task to do in the qtile status bar and shows 
if it is active.

    Mouse callbacks:
        Left click start/stop the task.
        Right click displays a menu in rofi or dmenu with all available 
tasks to start/stop at selection.
        Rolling mousewheel on widget changes the showing task.

    """

    orientations = base.ORIENTATION_HORIZONTAL
    defaults = [
        ('config_file', '~/.taskrc', 'Default config place of taskwarrior'),
        ('selected_menu', 'rofi', 'Default menu selection of task is rofi'),
        ('update_interval', 0.5, 'Delay in seconds between updates'),
    ]

    def __init__(self, **config):
        super().__init__('', **config)
        self.add_defaults(TaskWarriorWidget.defaults)
        self.text = 'No tasks scheduled'
        self.tw = TaskWarrior(config_filename=self.config_file)
        self.pending_tasks = self.tw.filter_tasks(dict(status='pending'))
        self.started = ' inactive'
        self.num_id = 1
        self.menus = dict(rofi = Rofi, dmenu = DMenu)
        self.menu = self.menus[self.selected_menu]()

        self.add_callbacks({
            'Button 1': self.toggle_task,
            'Button 2': self.menu_task,
            'Button 4': self.next_task,
            'Button 5': self.previous_task
        })

    def poll(self):
        self.text = self.tw.get_task(id = self.num_id)[-1]['description']
        return self.text + self.started

    def button_press(self, x, y, button):
        super().button_press(self, x, y, button)

    def toggle_task(self):
        # Left click toggles(active, inactive) the task showed on the qtile 
bar
        try:
            self.tw.get_task(id = self.num_id)[-1]['start']
        except KeyError:
            self.tw.task_start(id = self.num_id)
            self.started = ' active'
        else:
            self.tw.task_stop(id = self.num_id)
            self.started = ' inactive'

    def menu_task(self):
        opts = {t['description']:t['id'] for t in self.pending_tasks}
        self.menu.prompt = 'Select the task to start/stop: '
        index = self.menu(opts).value
        self.num_id = self.pending_tasks[index]['id']
        self.toggle_task()

    def next_task(self):
        # Shows on the bar the next task in order of urgency
        self.num_id += 1 if self.num_id < len(self.pending_tasks) else 0

    def previous_task(self):
        # Shows on the bar the previous task in order of urgency
        self.num_id -= 1 if self.num_id > 1 else 
-(len(self.pending_tasks)-2)


Em domingo, 14 de março de 2021 às 15:45:31 UTC-3, elParaguayo escreveu:

> I'd use the self.add_callbacks method as this is what most widgets use.
>
> Do you have some code you can share? Much easier to help if we can see 
> your actual code, otherwise we're just guessing.
> On Sunday, 14 March 2021 at 18:30:22 UTC [email protected] wrote:
>
>> Hi everyone! So I'm working on a widget from scratch that shows 
>> taskwarrior tasks. I've already tweaked some other widgets to get a better 
>> notion of how it works and I'm much more confident now to create my own.
>> The widget is already displaying the first task on the bar, but for some 
>> reason I can't get the button callbacks to work. Is there a good way to 
>> debug those? Because I tested the functions and they were working as 
>> supposed to, but I couldn't test the callbacks itself properly.
>> I also saw that there are two main ways to make those callbacks. One is 
>> to add the self.add_callbacks(dict_of_callbacks), with the buttons as keys 
>> and functions as values respectively, and the other is identifying the 
>> buttons directly from the button_press function. Are there too many 
>> differences besides the first one seems more cleaner to me? I did inherited 
>> the button_press function from the upper class.
>> Oh my widget inherits from ThreadPool, by the way, and I set up the 
>> update_interval to something like 0.5.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"qtile-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/qtile-dev/b4e34590-827d-4b1d-9a3e-2439638958fbn%40googlegroups.com.

Reply via email to