Skip to content

Commit 16e3510

Browse files
committed
Add Screen to pythonnative.py
1 parent 9a3d431 commit 16e3510

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

apps/pythonnative_demo/app/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
def main():
5-
# Create a main view
6-
main_view = pn.View()
5+
# Create a screen
6+
screen = pn.Screen()
77

88
# Create a layout
99
layout = pn.LinearLayout()
@@ -16,11 +16,11 @@ def main():
1616
label = pn.Label("Hello, World!")
1717
layout.add_view(label)
1818

19-
# Add layout to main view
20-
main_view.add_view(layout)
19+
# Set layout to screen
20+
screen.set_layout(layout)
2121

22-
# Display the main view
23-
main_view.show()
22+
# Display the screen
23+
screen.show()
2424

2525

2626
if __name__ == "__main__":

libs/pythonnative/cli/pn.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def run_project(args: argparse.Namespace) -> None:
6666

6767
# Copy the user's Python code into the project
6868
src_dir = os.path.join(os.getcwd(), "app")
69-
dest_dir = os.path.join(build_dir, "app") # You might need to adjust this depending on the project structure
69+
dest_dir = os.path.join(
70+
build_dir, "app"
71+
) # You might need to adjust this depending on the project structure
7072
shutil.copytree(src_dir, dest_dir, dirs_exist_ok=True)
7173

7274
# Install any necessary Python packages into the project environment
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .pythonnative import Button, Label, LinearLayout, Screen
2+
3+
__all__ = ["Button", "Label", "LinearLayout", "Screen"]

libs/pythonnative/pythonnative/pythonnative.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,26 @@ class PlatformNotDetectedError(Exception):
1212
if system == "iOS":
1313
from rubicon.objc import ObjCClass
1414

15-
# Map native iOS classes to PythonNative classes
15+
class Screen:
16+
native_class = ObjCClass("UIViewController")
17+
18+
def __init__(self):
19+
self.native_instance = self.native_class.alloc().init()
20+
self.layout = None
21+
22+
def add_view(self, view):
23+
if self.layout is None:
24+
raise ValueError("You must set a layout before adding views.")
25+
self.layout.add_view(view)
26+
27+
def set_layout(self, layout):
28+
self.layout = layout
29+
self.native_instance.view().addSubview_(layout.native_instance)
30+
31+
def show(self):
32+
# This method should contain code to present the ViewController
33+
pass
34+
1635
class Button:
1736
native_class = ObjCClass("UIButton")
1837

@@ -26,7 +45,6 @@ def set_title(self, title: str) -> None:
2645
def get_title(self) -> str:
2746
return self.native_instance.titleForState_(0)
2847

29-
3048
class Label:
3149
native_class = ObjCClass("UILabel")
3250

@@ -40,25 +58,43 @@ def set_text(self, text: str) -> None:
4058
def get_text(self) -> str:
4159
return self.native_instance.text()
4260

43-
4461
class LinearLayout:
4562
native_class = ObjCClass("UIStackView")
4663

4764
def __init__(self) -> None:
4865
self.native_instance = self.native_class.alloc().initWithFrame_(
49-
((0, 0), (0, 0)))
66+
((0, 0), (0, 0))
67+
)
5068
self.native_instance.setAxis_(0) # Set axis to vertical
5169
self.views = []
5270

5371
def add_view(self, view):
5472
self.views.append(view)
5573
self.native_instance.addArrangedSubview_(view.native_instance)
5674

57-
5875
elif system == "Android":
5976
from java import jclass
6077

61-
# Map native Android classes to PythonNative classes
78+
class Screen:
79+
native_class = jclass("android.app.Activity")
80+
81+
def __init__(self):
82+
self.native_instance = self.native_class()
83+
self.layout = None
84+
85+
def add_view(self, view):
86+
if self.layout is None:
87+
raise ValueError("You must set a layout before adding views.")
88+
self.layout.add_view(view)
89+
90+
def set_layout(self, layout):
91+
self.layout = layout
92+
self.native_instance.setContentView(layout.native_instance)
93+
94+
def show(self):
95+
# This method should contain code to start the Activity
96+
pass
97+
6298
class Button:
6399
native_class = jclass("android.widget.Button")
64100

@@ -72,7 +108,6 @@ def set_title(self, title: str) -> None:
72108
def get_title(self) -> str:
73109
return self.native_instance.getText().toString()
74110

75-
76111
class Label:
77112
native_class = jclass("android.widget.TextView")
78113

@@ -86,20 +121,17 @@ def set_text(self, text: str) -> None:
86121
def get_text(self) -> str:
87122
return self.native_instance.getText().toString()
88123

89-
90124
class LinearLayout:
91125
native_class = jclass("android.widget.LinearLayout")
92126

93127
def __init__(self) -> None:
94128
self.native_instance = self.native_class()
95-
self.native_instance.setOrientation(
96-
1) # Set orientation to vertical
129+
self.native_instance.setOrientation(1) # Set orientation to vertical
97130
self.views = []
98131

99132
def add_view(self, view):
100133
self.views.append(view)
101134
self.native_instance.addView(view.native_instance)
102135

103-
104136
else:
105137
raise PlatformNotDetectedError("Platform could not be detected or is unsupported.")

0 commit comments

Comments
 (0)