Skip to content

Commit

Permalink
Clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Apr 29, 2022
1 parent 8635793 commit c1dc6cc
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 17 deletions.
8 changes: 7 additions & 1 deletion client/lib/controls/clipboard.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

import '../models/control.dart';
Expand All @@ -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();
Expand Down
24 changes: 21 additions & 3 deletions sdk/python/flet/clipboard.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
23 changes: 12 additions & 11 deletions sdk/python/playground/icons-browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
GridView,
Icon,
IconButton,
ListView,
OutlinedButton,
Page,
Row,
SnackBar,
Text,
TextButton,
TextField,
Theme,
alignment,
border,
border_radius,
colors,
icons,
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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,
)
)

Expand Down
18 changes: 18 additions & 0 deletions sdk/python/playground/replace-test.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions sdk/python/playground/simple-snack.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions sdk/python/playground/snackbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c1dc6cc

Please sign in to comment.