Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User control #26

Merged
merged 6 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ skip_commits:
- "*.md"

environment:
GO_VERSION: 1.18.1
GO_VERSION: 1.18.3
GO_TAGS: --tags release
python_version: 3.10
GITHUB_TOKEN:
Expand Down
2 changes: 2 additions & 0 deletions client/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<title>Flet</title>
<link rel="manifest" href="manifest.json">

<!-- flutterWebRenderer -->

<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = null;
Expand Down
1 change: 1 addition & 0 deletions sdk/python/flet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
from flet.text_button import TextButton
from flet.textfield import TextField
from flet.theme import Theme
from flet.user_control import UserControl
from flet.vertical_divider import VerticalDivider
3 changes: 3 additions & 0 deletions sdk/python/flet/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def __init__(
def _get_control_name(self):
return "clipboard"

def _is_isolated(self):
return True

# value
@property
def value(self):
Expand Down
14 changes: 11 additions & 3 deletions sdk/python/flet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ def __init__(
if ref:
ref.current = self

def _assign(self, variable):
variable = self
def _is_isolated(self):
return False

def did_mount(self):
pass

def will_unmount(self):
pass

def _get_children(self):
return []
Expand Down Expand Up @@ -306,7 +312,8 @@ def build_update_commands(self, index, added_controls, commands):
# unchanged control
for h in previous_ints[a1:a2]:
ctrl = hashes[h]
ctrl.build_update_commands(index, added_controls, commands)
if not ctrl._is_isolated():
ctrl.build_update_commands(index, added_controls, commands)
n += 1
elif tag == "replace":
ids = []
Expand Down Expand Up @@ -358,6 +365,7 @@ def _remove_control_recursively(self, index, control):
self._remove_control_recursively(index, child)

if control.__uid in index:
control.will_unmount()
del index[control.__uid]

# private methods
Expand Down
21 changes: 18 additions & 3 deletions sdk/python/flet/flet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,24 @@
FLET_APP,
]

WebRenderer = Literal[None, "auto", "html", "canvaskit"]


def page(
name="", port=0, permissions=None, view: AppViewer = WEB_BROWSER, assets_dir=None
name="",
port=0,
permissions=None,
view: AppViewer = WEB_BROWSER,
assets_dir=None,
web_renderer=None,
):
conn = _connect_internal(
page_name=name,
port=port,
is_app=False,
permissions=permissions,
assets_dir=assets_dir,
web_renderer=web_renderer,
)
print("Page URL:", conn.page_url)
page = Page(conn, constants.ZERO_SESSION)
Expand All @@ -63,6 +71,7 @@ def app(
permissions=None,
view: AppViewer = FLET_APP,
assets_dir=None,
web_renderer=None,
):

if target == None:
Expand All @@ -75,6 +84,7 @@ def app(
permissions=permissions,
session_handler=target,
assets_dir=assets_dir,
web_renderer=web_renderer,
)
print("App URL:", conn.page_url)

Expand Down Expand Up @@ -129,6 +139,7 @@ def _connect_internal(
permissions=None,
session_handler=None,
assets_dir=None,
web_renderer=None,
):
if share and server == None:
server = constants.HOSTED_SERVICE_URL
Expand All @@ -141,7 +152,7 @@ def _connect_internal(
# page with a custom port starts detached process
attached = False if not is_app and port != 0 else True

port = _start_flet_server(port, attached, assets_dir)
port = _start_flet_server(port, attached, assets_dir, web_renderer)
server = f"http://localhost:{port}"

connected = threading.Event()
Expand Down Expand Up @@ -209,7 +220,7 @@ def _on_ws_failed_connect():
return conn


def _start_flet_server(port, attached, assets_dir):
def _start_flet_server(port, attached, assets_dir, web_renderer):

if port == 0:
port = _get_free_tcp_port()
Expand Down Expand Up @@ -243,6 +254,10 @@ def _start_flet_server(port, attached, assets_dir):
logging.info(f"Assets path configured: {assets_dir}")
fletd_env["FLET_STATIC_ROOT_DIR"] = assets_dir

if web_renderer not in [None, "", "auto"]:
logging.info(f"Web renderer configured: {web_renderer}")
fletd_env["FLET_WEB_RENDERER"] = web_renderer

args = [fletd_path, "--port", str(port)]

creationflags = 0
Expand Down
24 changes: 14 additions & 10 deletions sdk/python/flet/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def __update(self, *controls):

# add to index
self._index[id] = added_controls[n]

# call Control.did_mount
added_controls[n].did_mount()

n += 1

def add(self, *controls):
Expand Down Expand Up @@ -219,6 +223,16 @@ def _send_command(self, name: str, values: List[str]):
Command(0, name, values, None, None),
)

@beartype
def set_clipboard(self, value: str):
self.__offstage.clipboard.value = value
self.__offstage.clipboard.update()

@beartype
def show_snack_bar(self, snack_bar: SnackBar):
self.__offstage.snack_bar = snack_bar
self.__offstage.update()

# url
@property
def url(self):
Expand Down Expand Up @@ -341,16 +355,6 @@ def fonts(self, value: Optional[Dict[str, str]]):
self.__fonts = value
self._set_attr_json("fonts", value)

# clipboard
@property
def clipboard(self):
return self.__offstage.clipboard.value

@clipboard.setter
@beartype
def clipboard(self, value: Optional[str]):
self.__offstage.clipboard.value = value

# splash
@property
def splash(self):
Expand Down
26 changes: 26 additions & 0 deletions sdk/python/flet/user_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import List

from flet.control import Control
from flet.stack import Stack


class UserControl(Stack):
def __init__(self):
super().__init__()
content = self.build()
if isinstance(content, Control):
self.controls = [content]
elif isinstance(content, List) and all(
isinstance(control, Control) for control in content
):
self.controls = content
else:
raise Exception(
f"{self.__class__.__name__}.build() method must be implemented and returning either Control or List[Control]."
)

def build(self):
pass

def _is_isolated(self):
return True
5 changes: 5 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (

// development
staticRootDir = "STATIC_ROOT_DIR"
webRenderer = "WEB_RENDERER"
)

func init() {
Expand Down Expand Up @@ -278,3 +279,7 @@ func MasterSecretKey() string {
func StaticRootDir() string {
return viper.GetString(staticRootDir)
}

func WebRenderer() string {
return viper.GetString(webRenderer)
}
17 changes: 13 additions & 4 deletions server/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,19 @@ func Start(ctx context.Context, wg *sync.WaitGroup, serverPort int) {
index, _ := assetsFS.Open(siteDefaultDocument)
indexData, _ := ioutil.ReadAll(index)

c.Data(http.StatusOK, "text/html",
bytes.Replace(indexData,
[]byte("<base href=\"/\">"),
[]byte("<base href=\""+urlPath+"\">"), 1))
// base path
indexData = bytes.Replace(indexData,
[]byte("<base href=\"/\">"),
[]byte("<base href=\""+urlPath+"\">"), 1)

// web renderer
if config.WebRenderer() != "" {
indexData = bytes.Replace(indexData,
[]byte("<!-- flutterWebRenderer -->"),
[]byte("<script>window.flutterWebRenderer=\""+config.WebRenderer()+"\";</script>"), 1)
}

c.Data(http.StatusOK, "text/html", indexData)
} else {
// API not found
c.JSON(http.StatusNotFound, gin.H{
Expand Down