summaryrefslogtreecommitdiffstats
path: root/girok/calendar_cli/sidebar.py
blob: 19a9772e177b7e311bcb964593d656212b82d648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widgets import Button, Footer, Header, Static, Label, Placeholder, Tree
from textual.messages import Message
from textual.widget import Widget
from textual.reactive import var, reactive
from textual import events

import girok.api.category as category_api
import girok.api.task as task_api
import girok.utils.calendar as calendar_utils
import girok.utils.general as general_utils
import girok.constants as constants
from rich.style import Style
from rich.text import Text
from textual import log
from textual.widgets._tree import TreeNode

class CategoryTree(Tree):
    CSS_PATH = "./demo_dock.css"
    cats = dict()
    can_focus = True
    can_focus_children = True
    auto_expand=False
    highlighted_node = None
    selected_node = None
    init_select = False
    init_highlight = False
    class CategoryChanged(Message):
        def __init__(self, cat_path: str):
            super().__init__()
            self.cat_path = cat_path

    class CustomTestMessage(Message):
        def __init__(self):
            super().__init__()
    
    def on_mount(self):
        self.highlighted_node = self.root
        self.selected_node = self.root
        calendar_utils.add_left_arrow_tree(self.highlighted_node)

        self.line = 0
        self.cats = category_api.get_categories()
        self.root.expand()
        
        for cat in self.cats:
            top_cat = self.root.add(cat, expand=True, data={"color": self.cats[cat]['color']})
            top_cat.allow_expand = True
            calendar_utils.build_category_tree(top_cat, self.cats[cat]['subcategories'])
            
    def on_key(self, evt):
        if evt.key == "j":
            self.action_cursor_down()
        elif evt.key == "k":
            self.action_cursor_up()
        elif evt.key == "o":
            self.action_select_cursor()

    def render_label(self, node, base_style: Style, style: Style):
        node_label = node._label.copy()
        icon = ""
        if node.parent is None:
            icon = "📖 "
        elif node.parent.parent is None:
            icon = Text("● ", style=constants.CIRCLE_COLOR[node.data['color']])
        text = Text()
        text.append(icon)
        text.append(node_label)
        return text
        
    def on_tree_node_selected(self, event: Tree.NodeSelected):
        event.stop()
        full_cat_path = calendar_utils.get_full_path_from_node(event.node)
        self.selected_node = event.node
        self.post_message(self.CategoryChanged(full_cat_path))
        
    def on_tree_node_highlighted(self, event: Tree.NodeHighlighted):
        event.stop()
        prev_highlighted_node = self.highlighted_node
        calendar_utils.remove_left_arrow_tree(prev_highlighted_node)
        calendar_utils.remove_highlight(prev_highlighted_node)
        calendar_utils.add_left_arrow_tree(event.node)
        calendar_utils.add_highlight(event.node)
        self.highlighted_node = event.node
        
    def on_focus(self, evt):
        calendar_utils.add_left_arrow_tree(self.highlighted_node)
        
        
class TagTree(Tree):
    CSS_PATH = "./demo_dock.css"
    tags = []
    can_focus = True
    can_focus_children = True
    auto_expand=False
    highlighted_node = None
    selected_node = None
    
    class TagChanged(Message):
        def __init__(self, tag: str):
            super().__init__()
            self.tag = tag
            
    class CustomTestMessage(Message):
        def __init__(self):
            super().__init__()
            
    def on_mount(self):
        self.select_node(self.root)
        self.action_select_cursor()
        self.highlighted_node = self.root
        self.selected_node = self.root
        
        tags = task_api.get_tags()
        self.tags = tags
        
        self.root.expand()
        
        for tag in self.tags:
            self.root.add(tag, expand=True)
            
    def on_key(self, evt):
        if evt.key == "j":
            self.action_cursor_down()
        elif evt.key == "k":
            self.action_cursor_up()
        elif evt.key == "o":
            self.action_select_cursor()
            
    def on_focus(self, evt):
        calendar_utils.add_left_arrow_tree(self.highlighted_node)
            
    def render_label(self, node, base_style: Style, style: Style):
        node_label = node._label.copy()
        
        icon = ""
        if node.parent is None:
            icon = "📖 "
        elif node.parent.parent is None:
            icon = Text("● ", style="white")
                
        text = Text()
        text.append(icon)
        text.append(node_label)
        return text
        
    def on_tree_node_selected(self, event: Tree.NodeSelected):
        event.stop()
        # calendar_utils.remove_highlight(self.selected_node)
        tag = str(event.node._label)
        if tag.endswith(" " + constants.LEFT_ARROW_EMOJI):
            tag = tag[:-2]

        self.post_message(self.TagChanged(tag))
        event.node.set_label(tag)
        self.selected_node = event.node
        
    def on_tree_node_highlighted(self, event: Tree.NodeHighlighted):
        event.stop()
        prev_highlighted_node = self.highlighted_node
        calendar_utils.remove_highlight(prev_highlighted_node)
        calendar_utils.remove_left_arrow_tree(prev_highlighted_node)
        calendar_utils.add_left_arrow_tree(event.node)
        calendar_utils.add_highlight(event.node)
        self.highlighted_node = event.node
        
        
class SidebarMainContainer(Vertical):
    CSS_PATH = "./demo_dock.css"
    def compose(self):
        yield CategoryTree("All Categories", id="sidebar")
        yield TagTree("All Tags", id="tag-tree")
    
    
class SidebarContainer(Vertical):
    CSS_PATH = "./demo_dock.css"
        
    def compose(self):
        yield SidebarMainContainer(id="sidebar-main-container")