1
+ from datetime import datetime
1
2
from typing import TYPE_CHECKING
2
3
from warnings import warn
3
- from ..facet import Facet
4
+ from pathlib import Path
5
+
6
+ from textual .widgets import (Checkbox , Footer , Header , Input , Label ,
7
+ RadioButton , Static )
8
+
9
+ from humanize import naturalsize
10
+
11
+ from ..exceptions import DependencyRequired
12
+ from ..facet import Facet , Image , LayoutElement
4
13
if TYPE_CHECKING :
5
14
from .textual_adaptor import TextualAdaptor
6
15
@@ -22,6 +31,30 @@ def set_title(self, title: str):
22
31
# NOTE: When you receive Facet in Command.init, the app does not exist yet
23
32
warn ("Setting textual title not implemented well." )
24
33
34
+ def _layout (self , elements : list [LayoutElement ]):
35
+ append = self .adaptor .layout_elements .append
36
+ try :
37
+ from PIL import Image as ImagePIL
38
+ from textual_imageview .viewer import ImageViewer
39
+ PIL = True
40
+ except :
41
+ PIL = False
42
+
43
+ for el in elements :
44
+ match el :
45
+ case Image ():
46
+ if not PIL :
47
+ raise DependencyRequired ("img" )
48
+ append (ImageViewer (ImagePIL .open (el .src )))
49
+ case Path ():
50
+ size = naturalsize (el .stat ().st_size )
51
+ mtime = datetime .fromtimestamp (el .stat ().st_mtime ).strftime ("%Y-%m-%d %H:%M:%S" )
52
+ append (Label (f"{ el } / { size } / { mtime } " ))
53
+ case str ():
54
+ append (Label (el ))
55
+ case _:
56
+ append (Label ("Error in the layout: Unknown {el}" ))
57
+
25
58
def submit (self , * args , ** kwargs ):
26
59
super ().submit (* args , ** kwargs )
27
60
try :
0 commit comments