diff --git a/client/lib/controls/clipboard.dart b/client/lib/controls/clipboard.dart index a6d514707..25dea0148 100644 --- a/client/lib/controls/clipboard.dart +++ b/client/lib/controls/clipboard.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; + +import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import '../models/control.dart'; @@ -16,7 +19,10 @@ class ClipboardControl extends StatelessWidget { var value = control.attrString("value"); if (value != null) { - debugPrint("Clipboard value: $value"); + debugPrint("Clipboard JSON value: $value"); + + var jv = json.decode(value); + Clipboard.setData(ClipboardData(text: jv["d"] as String?)); } return const SizedBox.shrink(); diff --git a/sdk/python/flet/clipboard.py b/sdk/python/flet/clipboard.py index 9a86942da..bb8a9f146 100644 --- a/sdk/python/flet/clipboard.py +++ b/sdk/python/flet/clipboard.py @@ -1,7 +1,22 @@ +import dataclasses +import json +import time +from typing import Optional + +from beartype._decor.main import beartype + from flet.control import Control +from flet.embed_json_encoder import EmbedJsonEncoder from flet.ref import Ref +@beartype +@dataclasses.dataclass +class ClipboardData: + ts: str + d: Optional[str] + + class Clipboard(Control): def __init__( self, @@ -27,8 +42,11 @@ def _get_control_name(self): # value @property def value(self): - return self._get_attr("value") + return self.__value @value.setter - def value(self, value): - self._set_attr("value", value) + @beartype + def value(self, value: Optional[str]): + self.__value = value + cd = ClipboardData(str(time.time()), value) + self._set_attr("value", json.dumps(cd, cls=EmbedJsonEncoder) if value else None) diff --git a/sdk/python/playground/icons-browser.py b/sdk/python/playground/icons-browser.py index ca0056a10..17fd89ec8 100644 --- a/sdk/python/playground/icons-browser.py +++ b/sdk/python/playground/icons-browser.py @@ -11,16 +11,13 @@ GridView, Icon, IconButton, - ListView, OutlinedButton, Page, Row, + SnackBar, Text, - TextButton, TextField, - Theme, alignment, - border, border_radius, colors, icons, @@ -32,8 +29,8 @@ # fetch all icon constants from icons.py module icons_list = [] list_started = False -for name, value in vars(icons).items(): - if name == "TEN_K": +for key, value in vars(icons).items(): + if key == "TEN_K": list_started = True if list_started: icons_list.append(value) @@ -56,8 +53,10 @@ def main(page: Page): status_bar = Text() def copy_to_clipboard(e): - print("Copy to clipboard:", e.control.data) + icon_key = e.control.data + print("Copy to clipboard:", icon_key) page.clipboard = e.control.data + page.snack_bar = SnackBar(Text(f"Copied {icon_key}"), open=True) page.update() def display_icons(search_term: str): @@ -68,16 +67,18 @@ def display_icons(search_term: str): # add matching icons for i in range(0, len(icons_list)): if search_term != "" and search_term in icons_list[i]: + icon_name = icons_list[i] + icon_key = f"icons.{icon_name.upper()}" search_results.controls.append( OutlinedButton( content=Container( content=Column( [ Icon( - name=icons_list[i], + name=icon_name, ), Text( - value=icons_list[i], + value=icon_name, size=10, width=100, # selectable=True, @@ -96,9 +97,9 @@ def display_icons(search_term: str): # bgcolor="#f0f0f0", border_radius=border_radius.all(3), ), - tooltip="Click to copy icon name to a clipboard", + tooltip=f"{icon_key}\nClick to copy to a clipboard", on_click=copy_to_clipboard, - data=icons_list[i], + data=icon_key, ) ) diff --git a/sdk/python/playground/replace-test.py b/sdk/python/playground/replace-test.py new file mode 100644 index 000000000..5fb19fd99 --- /dev/null +++ b/sdk/python/playground/replace-test.py @@ -0,0 +1,18 @@ +import time + +import flet +from flet import Container, ElevatedButton, Text + + +def main(page): + + c = Container(content=Text("A")) + + def btn_click(e): + c.content = Text(str(time.time())) + page.update() + + page.add(c, ElevatedButton("Replace!", on_click=btn_click)) + + +flet.app(name="test1", port=8550, target=main, view=flet.WEB_BROWSER) diff --git a/sdk/python/playground/simple-snack.py b/sdk/python/playground/simple-snack.py new file mode 100644 index 000000000..a3c029942 --- /dev/null +++ b/sdk/python/playground/simple-snack.py @@ -0,0 +1,35 @@ +import logging + +import flet +from flet import ElevatedButton, SnackBar, Text + +logging.basicConfig(level=logging.DEBUG) + + +class Data: + def __init__(self) -> None: + self.counter = 0 + + +d = Data() + + +def main(page): + + page.snack_bar = SnackBar( + content=Text("Hello, world!"), + # remove_current_snackbar=True, + action="Alright!", + ) + + def on_click(e): + # page.snack_bar.content.value = f"Hello, world: {d.counter}" + page.snack_bar = SnackBar(Text(f"Hello {d.counter}")) + page.snack_bar.open = True + d.counter += 1 + page.update() + + page.add(ElevatedButton("Open SnackBar", on_click=on_click)) + + +flet.app(name="test1", port=8550, target=main, view=flet.WEB_BROWSER) diff --git a/sdk/python/playground/snackbar.py b/sdk/python/playground/snackbar.py index 75327930a..f6b080408 100644 --- a/sdk/python/playground/snackbar.py +++ b/sdk/python/playground/snackbar.py @@ -23,13 +23,14 @@ def action_click(e): page.snack_bar = SnackBar( content=Text("Hello, world!"), - remove_current_snackbar=True, + # remove_current_snackbar=True, action="Alright!", on_action=action_click, ) def on_click(e): - page.snack_bar.content.value = f"Hello, world: {d.counter}" + # page.snack_bar.content.value = f"Hello, world: {d.counter}" + page.snack_bar.content = Text(f"Hello, world: {d.counter}") page.snack_bar.open = True d.counter += 1 page.update()