-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnotepad.v
82 lines (67 loc) · 4.16 KB
/
notepad.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import malisipi.mui as m
import os
const open_file_emoji="⤵"
const save_file_emoji="💾"
struct AppData {
mut:
file_path string //=""
}
fn about_dialog(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
m.messagebox("Notepad","A Notepad Created for MUI Examples","ok","info")
}
fn load_file(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
mut file_path:=event_details.value
if event_details.target_type=="menubar" || event_details.target_type=="button"{
file_path=m.openfiledialog("Notepad")
if file_path=="" { return }
}
if os.exists(file_path){
file_text:=os.read_file(file_path) or { m.messagebox("Notepad","Unable to open `"+file_path+"`\n* You may not have read permission.","ok","error") return }
app_data.file_path=file_path
unsafe { app.get_object_by_id("textarea")[0]["text"].str=file_text }
} else {
m.messagebox("Notepad","Unable to open `"+file_path+"`\n* File not found.","ok","error")
}
}
fn save_file(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
if app_data.file_path!="" {
unsafe { os.write_file(app_data.file_path, app.get_object_by_id("textarea")[0]["text"].str.replace("\0","")) or { m.messagebox("Notepad","Unable to write `"+app_data.file_path+"`\n* You may not have write permission.","ok","error") return } }
} else {
save_as_file(event_details, mut app, mut app_data)
}
}
fn save_as_file(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
file_path:=m.savefiledialog("Notepad")
if file_path=="" { return }
unsafe { os.write_file(file_path, app.get_object_by_id("textarea")[0]["text"].str.replace("\0","")) or { m.messagebox("Notepad","Unable to write `"+file_path+"`\n* You may not have write permission.","ok","error") return } }
}
fn change_codefield(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
unsafe { app.get_object_by_id("textarea")[0]["code"].bol=event_details.value=="true" }
}
fn change_text_size(event_details m.EventDetails, mut app &m.Window, mut app_data &AppData){
unsafe { app.get_object_by_id("textarea")[0]["tSize"].num=event_details.value.int() }
}
menubar:=[
{"text":m.WindowData{str:"File"}, "items":m.WindowData{lst:[
{"text":m.WindowData{str:"Open"}, "fn": m.WindowData{fun:load_file}}
{"text":m.WindowData{str:"Save"}, "fn": m.WindowData{fun:save_file}}
{"text":m.WindowData{str:"Save As"}, "fn": m.WindowData{fun:save_as_file}}
]}},
{"text":m.WindowData{str:"About"}, "items":m.WindowData{lst:[
{"text":m.WindowData{str:"About"}, "fn": m.WindowData{fun:about_dialog}},
{"text":m.WindowData{str:"About MUI"}, "fn": m.WindowData{fun:m.about_dialog}}
]}},
]
mut app:=m.create(m.WindowConfig{ title:"Notepad - MUI Examples", width:400, height: 300, menubar:menubar, file_handler:load_file, ask_quit:true, app_data:&AppData{}, toolbar:25, statusbar:25, draw_mode:.system_native })
unsafe {
app.get_object_by_id("@toolbar")[0]["bg"].clr = app.color_scheme[0]
app.get_object_by_id("@statusbar")[0]["bg"].clr = app.color_scheme[0]
}
app.button(id:"open", x:0, y:0 width:25, height:25, text:open_file_emoji, onclick:load_file, icon:true, frame:"@toolbar")
app.button(id:"save", x:30, y:0 width:25, height:25, text:save_file_emoji, onclick:save_file, icon:true, frame:"@toolbar")
app.switch(id:"codefield", x:60, y:5 width:30, height:15, text:"Codefield", onchange:change_codefield, frame:"@toolbar")
app.slider(id:"text_size", x:180, y:5 width:80, height:15, value_min:8, value_max:40, value:20, onclick:change_text_size, onchange:change_text_size, onunclick:change_text_size, frame:"@toolbar")
app.label(id:"status_info", x:0, y:0, height:25, width:"100%x" text_align:0, text:"Welcome to Notepad!", frame:"@statusbar")
app.textarea(id:"textarea", x:0, y:0, width:"100%x -15", height:"100%y", placeholder:"Open/Drop a file to edit\nOr create a new file")
app.scrollbar(id:"textarea_scrollbar", x:"# 0", y:"# 0", width: 15, height:"100%y", vertical:true, connected_widget:app.get_object_by_id("textarea")[0], value_min:0, value_max:250, value:0)
app.run()