mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
implemented add_areas
This commit is contained in:
@@ -12,21 +12,25 @@ class GridTest(App):
|
||||
async def on_startup(self, event: events.Startup) -> None:
|
||||
|
||||
layout = GridLayout()
|
||||
view = await self.push_view(View(layout=layout))
|
||||
await self.push_view(View(layout=layout))
|
||||
|
||||
layout.add_column(name="col", max_size=20, repeat=4)
|
||||
layout.add_row(name="numbers", max_size=10)
|
||||
layout.add_row(name="row", max_size=10, repeat=4)
|
||||
|
||||
layout.add_area("numbers", ("col1-start", "col4-end"), "numbers")
|
||||
layout.add_area("zero", ("col1-start", "col2-end"), "row4")
|
||||
layout.add_area("dot", "col3", "row4")
|
||||
layout.add_area("equals", "col4", "row4")
|
||||
layout.add_areas(
|
||||
numbers="col1-start|col4-end,numbers",
|
||||
zero="col1-start|col2-end,row4",
|
||||
dot="col3,row4",
|
||||
equals="col4,row4",
|
||||
)
|
||||
|
||||
layout.add_widget(Placeholder(name="numbers"), area="numbers")
|
||||
layout.add_widget(Placeholder(name="0"), area="zero")
|
||||
layout.add_widget(Placeholder(name="."), area="dot")
|
||||
layout.add_widget(Placeholder(name="="), area="equals")
|
||||
layout.place(
|
||||
numbers=Placeholder(name="numbers"),
|
||||
zero=Placeholder(name="0"),
|
||||
dot=Placeholder(name="."),
|
||||
equals=Placeholder(name="="),
|
||||
)
|
||||
|
||||
layout.set_gap(1)
|
||||
layout.set_gutter(1)
|
||||
|
||||
@@ -108,9 +108,9 @@ class GridLayout(Layout):
|
||||
)
|
||||
)
|
||||
|
||||
def add_area(
|
||||
def _add_area(
|
||||
self, name: str, columns: str | tuple[str, str], rows: str | tuple[str, str]
|
||||
):
|
||||
) -> None:
|
||||
if isinstance(columns, str):
|
||||
column_start = f"{columns}-start"
|
||||
column_end = f"{columns}-end"
|
||||
@@ -123,8 +123,21 @@ class GridLayout(Layout):
|
||||
else:
|
||||
row_start, row_end = rows
|
||||
|
||||
area = GridArea(column_start, column_end, row_start, row_end)
|
||||
self.areas[name] = area
|
||||
self.areas[name] = GridArea(column_start, column_end, row_start, row_end)
|
||||
|
||||
def add_areas(self, **areas: str) -> None:
|
||||
for name, area in areas.items():
|
||||
area = area.replace(" ", "")
|
||||
column, _, row = area.partition(",")
|
||||
|
||||
column_start, column_sep, column_end = column.partition("|")
|
||||
row_start, row_sep, row_end = row.partition("|")
|
||||
|
||||
self._add_area(
|
||||
name,
|
||||
(column_start, column_end) if column_sep else column,
|
||||
(row_start, row_end) if row_sep else row,
|
||||
)
|
||||
|
||||
def set_gap(self, column: int, row: int | None = None) -> None:
|
||||
self.column_gap = column
|
||||
@@ -138,6 +151,13 @@ class GridLayout(Layout):
|
||||
self.widgets[widget] = area
|
||||
return widget
|
||||
|
||||
def place(self, *auto_widgets: Widget, **area_widgets: Widget) -> None:
|
||||
widgets = self.widgets
|
||||
for area, widget in area_widgets.items():
|
||||
widgets[widget] = area
|
||||
for widget in auto_widgets:
|
||||
widgets[widget] = None
|
||||
|
||||
def set_repeat(self, column: bool | None = None, row: bool | None = None) -> None:
|
||||
if column is not None:
|
||||
self.column_repeat = column
|
||||
|
||||
Reference in New Issue
Block a user