Skip to content

Commit

Permalink
add color button
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvladus committed May 16, 2024
1 parent 7465def commit 4f00dcc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
23 changes: 22 additions & 1 deletion data/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,25 @@ calendar {
calendar,
calendar header {
border: none;
}
}

colorbutton {
margin: 0px;
padding: 0px;
transform: scale(0.6);
}

colorbutton button {
border-radius: 9999px;
}

colorbutton button colorswatch {
all: unset;
border-radius: 9999px;
}

/* colorbutton button,
colorbutton colorswatch {
border-radius: 9999px;
margin: 0px;
} */
3 changes: 3 additions & 0 deletions errands/lib/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from errands.lib.gsettings import GSettings
from errands.lib.logging import Log
from errands.lib.utils import random_hex_color


@dataclass
Expand All @@ -45,6 +46,8 @@ class TaskListData:
def __post_init__(self):
if not self.uid:
self.uid = str(uuid4())
if not self.color:
self.color = random_hex_color()

def to_ical(self, single_task: str = None) -> str:
"""Build VTODO iCal component from TaskData properties"""
Expand Down
23 changes: 10 additions & 13 deletions errands/lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2023-2024 Vlad Krupinskii <[email protected]>
# SPDX-License-Identifier: MIT

import random
import time
from datetime import datetime
from functools import wraps
Expand All @@ -9,8 +10,6 @@

from gi.repository import GLib, Gtk # type:ignore

from errands.lib.logging import Log


def get_human_datetime(date_time: str) -> str:
if date_time:
Expand Down Expand Up @@ -58,17 +57,6 @@ def wrapper(*args, **kwargs):
return wrapper


def catch_errors(function: Callable):
"""Catch errors and log them"""

@wraps(function)
def wrapper(*args, **kwargs):
try:
function(*args, **kwargs)
except Exception as e:
Log.error(e)


def timeit(func):
@wraps(func)
def timeit_wrapper(*args, **kwargs):
Expand All @@ -81,3 +69,12 @@ def timeit_wrapper(*args, **kwargs):
return result

return timeit_wrapper


def rgb_to_hex(r: str, g: str, b: str) -> str:
return "#{:02x}{:02x}{:02x}".format(int(r), int(g), int(b))


def random_hex_color() -> str:
hex_chars: str = "0123456789ABCDEF"
return "#" + "".join(random.choice(hex_chars) for _ in range(6))
38 changes: 35 additions & 3 deletions errands/widgets/task_list/task_list_sidebar_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from errands.lib.gsettings import GSettings
from errands.lib.logging import Log
from errands.lib.sync.sync import Sync
from errands.lib.utils import rgb_to_hex
from errands.state import State
from errands.widgets.shared.components.boxes import ErrandsBox
from errands.widgets.shared.components.dialogs import ConfirmDialog
Expand All @@ -18,6 +19,8 @@


class TaskListSidebarRow(Gtk.ListBoxRow):
block_signals: bool = False

def __init__(self, list_data: TaskListData) -> None:
super().__init__()
self.list_data = list_data
Expand Down Expand Up @@ -149,6 +152,13 @@ def __build_ui(self) -> None:
drag_hover_ctrl.connect("enter", self._on_drop_hover)
self.add_controller(drag_hover_ctrl)

# Color button
color_dialog: Gtk.ColorDialog = Gtk.ColorDialog(with_alpha=False)
self.color_btn: Gtk.ColorDialogButton = Gtk.ColorDialogButton(
dialog=color_dialog
)
self.color_btn.connect("notify::rgba", self.__on_color_selected)

# Title
self.label: Gtk.Label = Gtk.Label(
hexpand=True, halign=Gtk.Align.START, ellipsize=3
Expand Down Expand Up @@ -181,9 +191,13 @@ def __build_ui(self) -> None:

self.set_child(
ErrandsBox(
spacing=12,
margin_start=6,
children=[self.label, self.size_counter, self.popover_menu],
spacing=6,
children=[
# self.color_btn,
self.label,
self.size_counter,
self.popover_menu,
],
)
)

Expand All @@ -198,6 +212,12 @@ def update_ui(self, update_task_list_ui: bool = True):
self.stack_page.set_name(self.name)
self.stack_page.set_title(self.name)

color: Gdk.RGBA = Gdk.RGBA()
color.parse(self.list_data.color)
self.block_signals = True
self.color_btn.set_rgba(color)
self.block_signals = False

# Update task list
if update_task_list_ui:
self.task_list.update_ui()
Expand Down Expand Up @@ -259,3 +279,15 @@ def _on_row_pressed(self, _gesture_click, _n_press, x, y) -> None:
position.y = y
self.popover_menu.set_pointing_to(position)
self.popover_menu.popup()

def __on_color_selected(self, btn: Gtk.ColorDialogButton, _):
if self.block_signals:
return
try:
r, g, b = btn.get_rgba().to_string().strip("rgba()").split(",")[:3]
color: str = rgb_to_hex(r, g, b)
except BaseException:
color: str = "#3584e4"
Log.debug(f"Task List '{self.list_data.uid}': Set color to '{color}'")
UserData.update_list_prop(self.list_data.uid, "color", color)
Sync.sync()

0 comments on commit 4f00dcc

Please sign in to comment.