Skip to content

Commit

Permalink
#986 introduce color mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Jun 12, 2023
1 parent 720a062 commit 8f8aaef
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 71 deletions.
33 changes: 0 additions & 33 deletions nicegui/colors.py

This file was deleted.

11 changes: 4 additions & 7 deletions nicegui/elements/avatar.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Optional

from ..colors import set_background_color, set_text_color
from ..element import Element
from .mixins.color_elements import BackgroundColorElement, TextColorElement


class Avatar(Element):
class Avatar(BackgroundColorElement, TextColorElement):
TEXT_COLOR_PROP = 'text-color'

def __init__(self,
icon: Optional[str] = None, *,
Expand All @@ -28,16 +28,13 @@ def __init__(self,
:param square: removes border-radius so borders are squared (default: False)
:param rounded: applies a small standard border-radius for a squared shape of the component (default: False)
"""
super().__init__('q-avatar')
super().__init__(tag='q-avatar', background_color=color, text_color=text_color)

if icon is not None:
self._props['icon'] = icon
self._props['square'] = square
self._props['rounded'] = rounded

set_background_color(self, color)
set_text_color(self, text_color, prop_name='text-color')

if size is not None:
self._props['size'] = size

Expand Down
9 changes: 4 additions & 5 deletions nicegui/elements/badge.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Optional

from ..colors import set_background_color, set_text_color
from .mixins.color_elements import BackgroundColorElement, TextColorElement
from .mixins.text_element import TextElement


class Badge(TextElement):
class Badge(TextElement, BackgroundColorElement, TextColorElement):
TEXT_COLOR_PROP = 'text-color'

def __init__(self,
text: str = '', *,
Expand All @@ -21,7 +22,5 @@ def __init__(self,
:param text_color: text color (either a Quasar, Tailwind, or CSS color or `None`, default: `None`)
:param outline: use 'outline' design (colored text and borders only) (default: False)
"""
super().__init__(tag='q-badge', text=text)
set_background_color(self, color)
set_text_color(self, text_color, prop_name='text-color')
super().__init__(tag='q-badge', text=text, text_color=text_color, background_color=color)
self._props['outline'] = outline
7 changes: 3 additions & 4 deletions nicegui/elements/button.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio
from typing import Any, Callable, Optional

from ..colors import set_background_color
from ..events import ClickEventArguments, handle_event
from .mixins.color_elements import BackgroundColorElement
from .mixins.disableable_element import DisableableElement
from .mixins.text_element import TextElement


class Button(TextElement, DisableableElement):
class Button(TextElement, DisableableElement, BackgroundColorElement):

def __init__(self,
text: str = '', *,
Expand All @@ -27,8 +27,7 @@ def __init__(self,
:param on_click: callback which is invoked when button is pressed
:param color: the color of the button (either a Quasar, Tailwind, or CSS color or `None`, default: 'primary')
"""
super().__init__(tag='q-btn', text=text)
set_background_color(self, color)
super().__init__(tag='q-btn', text=text, background_color=color)

if on_click:
self.on('click', lambda _: handle_event(on_click, ClickEventArguments(sender=self, client=self.client)))
Expand Down
9 changes: 3 additions & 6 deletions nicegui/elements/icon.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Optional

from ..colors import set_text_color
from ..element import Element
from .mixins.color_elements import TextColorElement


class Icon(Element):
class Icon(TextColorElement):

def __init__(self,
name: str,
Expand All @@ -22,10 +21,8 @@ def __init__(self,
:param size: size in CSS units, including unit name or standard size name (xs|sm|md|lg|xl), examples: 16px, 2rem
:param color: icon color (either a Quasar, Tailwind, or CSS color or `None`, default: `None`)
"""
super().__init__('q-icon')
super().__init__(tag='q-icon', text_color=color)
self._props['name'] = name

if size:
self._props['size'] = size

set_text_color(self, color)
7 changes: 3 additions & 4 deletions nicegui/elements/knob.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Optional

from ..colors import set_text_color
from .label import Label
from .mixins.color_elements import TextColorElement
from .mixins.disableable_element import DisableableElement
from .mixins.value_element import ValueElement


class Knob(ValueElement, DisableableElement):
class Knob(ValueElement, DisableableElement, TextColorElement):

def __init__(self,
value: float = 0.0,
Expand Down Expand Up @@ -35,12 +35,11 @@ def __init__(self,
:param size: size in CSS units, including unit name or standard size name (xs|sm|md|lg|xl), examples: 16px, 2rem
:param show_value: whether to show the value as text
"""
super().__init__(tag='q-knob', value=value, on_value_change=None, throttle=0.05)
super().__init__(tag='q-knob', value=value, on_value_change=None, throttle=0.05, text_color=color)

self._props['min'] = min
self._props['max'] = max
self._props['step'] = step
set_text_color(self, color)
self._props['show-value'] = True # NOTE: enable default slot, e.g. for nested icon

if center_color:
Expand Down
41 changes: 41 additions & 0 deletions nicegui/elements/mixins/color_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Any

from typing_extensions import get_args

from ...element import Element
from ...tailwind_types.background_color import BackgroundColor

QUASAR_COLORS = {'primary', 'secondary', 'accent', 'dark', 'positive', 'negative', 'info', 'warning'}
for color in {'red', 'pink', 'purple', 'deep-purple', 'indigo', 'blue', 'light-blue', 'cyan', 'teal', 'green',
'light-green', 'lime', 'yellow', 'amber', 'orange', 'deep-orange', 'brown', 'grey', 'blue-grey'}:
QUASAR_COLORS.add(color)
for i in range(1, 15):
QUASAR_COLORS.add(f'{color}-{i}')

TAILWIND_COLORS = get_args(BackgroundColor)


class BackgroundColorElement(Element):
BACKGROUND_COLOR_PROP = 'color'

def __init__(self, *, background_color: str, **kwargs: Any) -> None:
super().__init__(**kwargs)
if background_color in QUASAR_COLORS:
self._props[self.BACKGROUND_COLOR_PROP] = background_color
elif background_color in TAILWIND_COLORS:
self._classes.append(f'bg-{background_color}')
elif background_color is not None:
self._style['background-color'] = background_color


class TextColorElement(Element):
TEXT_COLOR_PROP = 'color'

def __init__(self, *, text_color: str, **kwargs: Any) -> None:
super().__init__(**kwargs)
if text_color in QUASAR_COLORS:
self._props[self.TEXT_COLOR_PROP] = text_color
elif text_color in TAILWIND_COLORS:
self._classes.append(f'text-{text_color}')
elif text_color is not None:
self._style['color'] = text_color
12 changes: 5 additions & 7 deletions nicegui/elements/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from nicegui import ui

from ..colors import set_text_color
from .mixins.color_elements import TextColorElement
from .mixins.value_element import ValueElement


class LinearProgress(ValueElement):
class LinearProgress(ValueElement, TextColorElement):
VALUE_PROP = 'value'

def __init__(self,
Expand All @@ -25,16 +25,15 @@ def __init__(self,
:param show_value: whether to show a value label in the center (default: `True`)
:param color: color (either a Quasar, Tailwind, or CSS color or `None`, default: "primary")
"""
super().__init__(tag='q-linear-progress', value=value, on_value_change=None)
super().__init__(tag='q-linear-progress', value=value, on_value_change=None, text_color=color)
self._props['size'] = size if size is not None else '20px' if show_value else '4px'
set_text_color(self, color)

if show_value:
with self:
ui.label().classes('absolute-center text-sm text-white').bind_text_from(self, 'value')


class CircularProgress(ValueElement):
class CircularProgress(ValueElement, TextColorElement):
VALUE_PROP = 'value'

def __init__(self,
Expand All @@ -57,12 +56,11 @@ def __init__(self,
:param show_value: whether to show a value label in the center (default: `True`)
:param color: color (either a Quasar, Tailwind, or CSS color or `None`, default: "primary")
"""
super().__init__(tag='q-circular-progress', value=value, on_value_change=None)
super().__init__(tag='q-circular-progress', value=value, on_value_change=None, text_color=color)
self._props['min'] = min
self._props['max'] = max
self._props['size'] = size
self._props['show-value'] = show_value
set_text_color(self, color)
self._props['track-color'] = 'grey-4'

if show_value:
Expand Down
8 changes: 3 additions & 5 deletions nicegui/elements/spinner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from typing_extensions import Literal

from ..colors import set_text_color
from ..element import Element
from .mixins.color_elements import TextColorElement

SpinnerTypes = Literal[
'default',
Expand Down Expand Up @@ -32,7 +31,7 @@
]


class Spinner(Element):
class Spinner(TextColorElement):

def __init__(self,
type: Optional[SpinnerTypes] = 'default', *,
Expand All @@ -49,7 +48,6 @@ def __init__(self,
:param color: color of the spinner (either a Quasar, Tailwind, or CSS color or `None`, default: "primary")
:param thickness: thickness of the spinner (applies to the "default" spinner only, default: 5.0)
"""
super().__init__('q-spinner' if type == 'default' else f'q-spinner-{type}')
super().__init__(tag='q-spinner' if type == 'default' else f'q-spinner-{type}', text_color=color)
self._props['size'] = size
set_text_color(self, color)
self._props['thickness'] = thickness

0 comments on commit 8f8aaef

Please sign in to comment.