Skip to content

Commit

Permalink
feat: impl api.layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Sep 26, 2024
1 parent ea58b6c commit d750fe2
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
9 changes: 8 additions & 1 deletion dooit/ui/api/api_components/layout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from textual.app import App
from dooit.ui.api.widgets import TodoLayout, WorkspaceLayout
from dooit.ui.widgets.trees import TodosTree, WorkspacesTree
from ._base import ApiComponent


class LayoutManager(ApiComponent):
def __init__(self) -> None:
def __init__(self, app: App) -> None:
self.app = app
self._todo_layout: TodoLayout = []
self._workspace_layout: WorkspaceLayout = []

Expand All @@ -14,6 +17,8 @@ def todo_layout(self) -> TodoLayout:
@todo_layout.setter
def todo_layout(self, layout: TodoLayout):
self._todo_layout = layout
for tree in self.app.query(TodosTree):
tree.refresh_options()

@property
def workspace_layout(self) -> WorkspaceLayout:
Expand All @@ -22,3 +27,5 @@ def workspace_layout(self) -> WorkspaceLayout:
@workspace_layout.setter
def workspace_layout(self, layout: WorkspaceLayout):
self._workspace_layout = layout
for tree in self.app.query(WorkspacesTree):
tree.refresh_options()
13 changes: 2 additions & 11 deletions dooit/ui/api/dooit_api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from typing import TYPE_CHECKING, List

from dooit.ui.api.plug import PluginManager
from dooit.ui.api.widgets import TodoLayout, WorkspaceLayout

from dooit.ui.events.events import DooitEvent, SwitchTab
from dooit.ui.registry import registry
from dooit.ui.widgets import ModelTree
from dooit.ui.widgets.bars import StatusBarWidget
from dooit.utils import CssManager

from .api_components import KeyManager
from .api_components import KeyManager, LayoutManager

if TYPE_CHECKING:
from ..tui import Dooit
Expand All @@ -22,6 +19,7 @@ def __init__(self, app: "Dooit") -> None:
self.plugin_manager.scan()
self.css_manager = CssManager()
self.keys = KeyManager(self.app.get_mode)
self.layouts = LayoutManager(self.app)

self.css_manager.refresh_css()

Expand Down Expand Up @@ -111,12 +109,5 @@ def start_search(self):
def start_sort(self):
self.focused.start_sort()

def set_workspace_layout(self, layout: WorkspaceLayout):
registry.set_workspace_layout(layout)
self.app.workspace_tree.refresh_options()

def set_todo_layout(self, layout: TodoLayout):
registry.set_todo_layout(layout)

def set_bar(self, widgets: List[StatusBarWidget]):
self.app.bar.set_widgets(widgets)
2 changes: 1 addition & 1 deletion dooit/ui/widgets/renderers/base_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def id(self) -> str:

@property
def table_layout(self) -> List:
raise NotImplementedError
return self.tree.layout

@property
def prompt(self) -> RenderableType:
Expand Down
5 changes: 0 additions & 5 deletions dooit/ui/widgets/renderers/todo_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Urgency,
)
from .base_renderer import BaseRenderer, Todo
from ...registry import registry


class TodoRender(BaseRenderer):
Expand All @@ -19,10 +18,6 @@ def model(self) -> Todo:
raise ValueError(f"Expected Todo, got {type(self._model)}")
return self._model

@property
def table_layout(self) -> List:
return registry.get_todo_layout()

def post_init(self):
self.description = TodoDescription(self.model)
self.due = Due(self.model)
Expand Down
4 changes: 0 additions & 4 deletions dooit/ui/widgets/renderers/workspace_renderer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import List
from .base_renderer import BaseRenderer, Workspace
from ..inputs.model_inputs import WorkspaceDescription
from ...registry import registry


class WorkspaceRender(BaseRenderer):
Expand All @@ -11,9 +10,6 @@ def model(self) -> Workspace:
raise ValueError(f"Expected Workspace, got {type(self._model)}")
return self._model

@property
def table_layout(self) -> List:
return registry.get_workspace_layout()

def post_init(self):
self.description = WorkspaceDescription(self.model)
6 changes: 5 additions & 1 deletion dooit/ui/widgets/trees/model_tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from typing import Generic, Iterable, Optional, TypeVar, Union
from typing import Any, Generic, Iterable, Optional, TypeVar, Union
from textual.widgets.option_list import Option
from dooit.api import Todo, Workspace
from dooit.ui.events.events import ModeChanged, ShowConfirm, StartSearch, StartSort
Expand Down Expand Up @@ -28,6 +28,10 @@ def __init__(self, model: ModelType, render_dict: RenderDictType) -> None:
self._renderers: RenderDictType = render_dict
self._filter_refresh = False

@property
def layout(self) -> Any:
raise NotImplementedError

@property
def filter_refresh(self):
return self._filter_refresh
Expand Down
4 changes: 4 additions & 0 deletions dooit/ui/widgets/trees/todos_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def _switch_to_workspace(self) -> None:

self.screen.query_one("WorkspacesTree").focus()

@property
def layout(self):
return self.app.api.layouts.todo_layout

def add_todo(self) -> str:
todo = self.model.add_todo()
render = TodoRender(todo, tree=self)
Expand Down
6 changes: 5 additions & 1 deletion dooit/ui/widgets/trees/workspaces_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import TYPE_CHECKING, List, Optional
from textual import on
from textual.widgets import ContentSwitcher
from textual.widgets.option_list import Option
Expand All @@ -22,6 +22,10 @@ def _get_parent(self, id: str) -> Optional[Workspace]:
def _get_children(self, id: str) -> List[Workspace]:
return Workspace.from_id(id).workspaces

@property
def layout(self):
return self.app.api.layouts.workspace_layout

@on(ModelTree.OptionHighlighted)
async def update_todo_tree(self, event: ModelTree.OptionHighlighted):
if not event.option_id:
Expand Down
5 changes: 2 additions & 3 deletions dooit/utils/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def key_setup(api: DooitAPI):
api.keys.set_normal("/", api.start_search)
api.keys.set_normal("ctrl+s", api.start_sort)

api.set_workspace_layout([WorkspaceWidget.description])

api.set_todo_layout([TodoWidget.description, TodoWidget.due])
api.layouts.workspace_layout = [WorkspaceWidget.description]
api.layouts.todo_layout = [TodoWidget.description, TodoWidget.due]

api.set_bar(bar_widgets)

0 comments on commit d750fe2

Please sign in to comment.