Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a49f291

Browse files
More native styling
1 parent 4f67a2c commit a49f291

File tree

6 files changed

+156
-19
lines changed

6 files changed

+156
-19
lines changed

src/bevyframe/Widgets/Style.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class Align:
2+
center = 'center'
3+
left = 'left'
4+
right = 'right'
5+
baseline = 'baseline'
6+
sub = 'sub'
7+
8+
9+
class Color:
10+
transparent = 'transparent'
11+
black = '#000000'
12+
white = '#ffffff'
13+
red = '#ff0000'
14+
hex = lambda x: f'#{x}'
15+
rgb = lambda r, g, b: f'rgb({r}, {g}, {b})'
16+
17+
18+
class Size:
19+
max_content = 'max-content'
20+
fit_content = 'fit-content'
21+
percent = lambda x: f'{x}%'
22+
pixel = lambda x: f'{x}px'
23+
auto = 'auto'
24+
25+
class Relative:
26+
font = lambda i: f'{i}em'
27+
x = lambda i: f'{i}ex'
28+
29+
class Viewport:
30+
height = lambda x: f'{x}vh'
31+
width = lambda x: f'{x}vw'
32+
min = lambda x: f'{x}vmin'
33+
max = lambda x: f'{x}vmax'
34+
35+
36+
class FourSided:
37+
def __init__(self, top=None, right=None, bottom=None, left=None):
38+
self.top = top
39+
self.right = right
40+
self.bottom = bottom
41+
self.left = left
42+
43+
44+
class Margin(FourSided):
45+
pass
46+
47+
48+
class Padding(FourSided):
49+
pass
50+
51+
52+
class Position:
53+
class absolute(FourSided):
54+
def __init__(self, top=None, right=None, bottom=None, left=None):
55+
self.item = 'absolute'
56+
super().__init__(top, right, bottom, left)
57+
58+
class relative(FourSided):
59+
def __init__(self, top=None, right=None, bottom=None, left=None):
60+
self.item = 'relative'
61+
super().__init__(top, right, bottom, left)
62+
63+
class fixed(FourSided):
64+
def __init__(self, top=None, right=None, bottom=None, left=None):
65+
self.item = 'fixed'
66+
super().__init__(top, right, bottom, left)
67+
68+
class sticky(FourSided):
69+
def __init__(self, top=None, right=None, bottom=None, left=None):
70+
self.item = 'sticky'
71+
super().__init__(top, right, bottom, left)
72+
73+
74+
def add_style(p1, p2):
75+
return f"calc({p1} + {p2})"
76+
77+
78+
def substract_style(p1, p2):
79+
return f"calc({p1} - {p2})"
80+
81+
82+
def multiply_style(p1, p2):
83+
return f"calc({p1} * {p2})"
84+
85+
86+
def divide_style(p1, p2):
87+
return f"calc({p1} / {p2})"

src/bevyframe/Widgets/Templates/Containers.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@
66
**kwargs
77
)
88

9-
Root = lambda childs, style = None, onclick = None: Container(
9+
Root = lambda childs, style = None, **kwargs: Container(
1010
selector='root',
1111
childs=childs if isinstance(childs, list) else [childs],
1212
style=style if style else {},
13-
onclick=onclick if onclick else ''
13+
**kwargs
1414
)
1515

16-
Box = lambda childs, style = None, onclick = None: Container(
16+
Box = lambda childs, style = None, onclick = None, **kwargs: Container(
1717
selector='the_box',
1818
childs=childs if isinstance(childs, list) else [childs],
1919
style=style if style else {},
20-
onclick=onclick if onclick else ''
20+
onclick=onclick if onclick else '',
21+
**kwargs
2122
)
2223

23-
Post = lambda childs, style = None, onclick = None: Container(
24+
Post = lambda childs, style = None, onclick = None, **kwargs: Container(
2425
selector='post',
2526
childs=childs if isinstance(childs, list) else [childs],
2627
style=style if style else {},
27-
onclick=onclick if onclick else ''
28+
onclick=onclick if onclick else '',
29+
**kwargs
2830
)
2931

3032
Line = lambda childs, style = None, onclick = None, **kwargs: Widget(

src/bevyframe/Widgets/Widget.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,64 @@
11
from bevyframe.Helpers.RenderCSS import RenderCSS
2+
from bevyframe.Widgets.Style import Margin, Padding, Position
23

34

45
class Widget:
5-
def __init__(self, item, **kwargs):
6+
def __init__(
7+
self,
8+
item,
9+
style: dict = None,
10+
css: dict = None,
11+
color: str = None,
12+
background_color: str = None,
13+
height: str = None,
14+
width: str = None,
15+
min_height: str = None,
16+
max_height: str = None,
17+
min_width: str = None,
18+
max_width: str = None,
19+
text_align: str = None,
20+
margin: (str, Margin) = None,
21+
padding: (str, Padding) = None,
22+
position: Position = None,
23+
border_radius: str = None,
24+
font_size: str = None,
25+
vertical_align: str = None,
26+
**kwargs,
27+
):
628
self.data = {}
729
self.element = item
8-
self.style = {}
30+
self.style = {} if style is None else style
31+
self.content = ''
32+
if style is None:
33+
if css is not None:
34+
self.style = css
35+
if isinstance(margin, str):
36+
self.style.update({'margin': margin})
37+
elif isinstance(margin, Margin):
38+
for i in ['top', 'right', 'bottom', 'left']:
39+
if getattr(margin, i) is not None:
40+
self.style.update({f'margin-{i}': getattr(margin, i)})
41+
if isinstance(padding, str):
42+
self.style.update({'padding': padding})
43+
elif isinstance(padding, Padding):
44+
for i in ['top', 'right', 'bottom', 'left']:
45+
if getattr(padding, i) is not None:
46+
self.style.update({f'padding-{i}': getattr(padding, i)})
47+
if position is not None:
48+
self.style.update({'position': position.item})
49+
for i in ['top', 'right', 'bottom', 'left']:
50+
if getattr(position, i) is not None:
51+
self.style.update({i: getattr(position, i)})
52+
k = [i for i in locals().keys()]
53+
for i in k:
54+
if i not in ['self', 'item', 'style', 'css', 'data', 'element', 'content', 'margin', 'padding', 'position', 'kwargs']:
55+
if locals().get(i, None) is not None:
56+
self.style.update({i.replace('_', '-'): locals()[i]})
957
for arg in kwargs:
1058
if arg == 'innertext':
1159
self.content = [kwargs['innertext']]
1260
elif arg == 'childs':
1361
self.content = kwargs['childs']
14-
elif arg == 'style':
15-
self.style = kwargs['style']
1662
else:
1763
self.data.update({arg: kwargs[arg]})
1864

2.36 KB
Binary file not shown.

src/bevyframe/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from bevyframe.Widgets.Page import Page
22
from bevyframe.Widgets.Widget import Widget
3+
from bevyframe.Widgets.Style import *
34
from bevyframe.Helpers.RenderCSS import RenderCSS
45
from bevyframe.Widgets.Templates import *
56
from bevyframe.Objects.Request import Request

tests/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ def get(request: Request) -> (Page, Response):
1212
selector=f'body_{request.user.id.settings.theme_color}',
1313
childs=[
1414
Title(f'Hello, {request.user.id.name} {request.user.id.surname} from {request.user.network}!'),
15-
Box(style={
16-
"width": "max-content",
17-
"text-align": "center"
18-
}, childs=[
19-
Line([Textbox('', type="text", placeholder='textbox')]),
20-
Line([Button(innertext='Button')]),
21-
Line([Button(selector='small', innertext='Button')]),
22-
Line([Button(selector='mini', innertext='Button')])
23-
])
15+
Box(
16+
width=Size.max_content,
17+
text_align=Align.center,
18+
childs=[
19+
Line([Textbox('', type="text", placeholder='textbox')]),
20+
Line([Button(innertext='Button')]),
21+
Line([Button(selector='small', innertext='Button')]),
22+
Line([Button(selector='mini', innertext='Button')])
23+
]
24+
)
2425
]
2526
)

0 commit comments

Comments
 (0)