Skip to content

Commit 13eaaea

Browse files
committed
Refactor notepad example
1 parent 56161ea commit 13eaaea

1 file changed

Lines changed: 64 additions & 42 deletions

File tree

examples/notepad.cr

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
require "../src/uing"
22

33
class NotepadApp
4-
@current_file_path : String?
5-
@is_modified : Bool
6-
@main_window : UIng::Window
4+
@file_path : String?
5+
@dirty : Bool
6+
@window : UIng::Window
77
@vbox : UIng::Box
88
@entry : UIng::MultilineEntry
99

1010
def initialize
11-
@current_file_path = nil
12-
@is_modified = false
11+
@file_path = nil
12+
@dirty = false
1313

1414
UIng.init
1515

@@ -19,108 +19,130 @@ class NotepadApp
1919
save_item = file_menu.append_item("Save")
2020
save_as_item = file_menu.append_item("Save As")
2121
file_menu.append_separator
22-
quit_item = file_menu.append_quit_item
22+
file_menu.append_quit_item
2323

2424
help_menu = UIng::Menu.new("Help")
2525
about_item = help_menu.append_about_item
2626

2727
# Now create UI components
28-
@main_window = UIng::Window.new("Notepad", 500, 300, menubar: true)
28+
@window = UIng::Window.new("Notepad", 500, 300, menubar: true)
2929
@vbox = UIng::Box.new(:vertical)
3030
@entry = UIng::MultilineEntry.new(wrapping: true)
3131

3232
# Set up the window layout
33-
@main_window.child = @vbox
33+
@window.child = @vbox
3434
@vbox.append @entry, true
3535

3636
# Set up event handlers
37-
setup_event_handlers(open_item, save_item, save_as_item, quit_item, about_item)
37+
bind_events(open_item, save_item, save_as_item, about_item)
3838

3939
# Initialize title
40-
update_title
40+
refresh_title
4141
end
4242

43-
def setup_event_handlers(open_item, save_item, save_as_item, quit_item, about_item)
43+
def bind_events(open_item, save_item, save_as_item, about_item)
44+
bind_entry_changed
45+
bind_open(open_item)
46+
bind_save(save_item)
47+
bind_save_as(save_as_item)
48+
bind_quit
49+
bind_about(about_item)
50+
bind_window_closing
51+
end
52+
53+
private def bind_entry_changed
4454
# Track text changes
4555
@entry.on_changed do
46-
@is_modified = true
47-
update_title
56+
@dirty = true
57+
refresh_title
4858
end
59+
end
4960

61+
private def bind_open(open_item)
5062
# Open file functionality
5163
open_item.on_clicked do |window|
52-
if file_path = @main_window.open_file
64+
if file_path = @window.open_file
5365
begin
5466
content = File.read(file_path)
5567
@entry.text = content
56-
@current_file_path = file_path
57-
@is_modified = false
58-
update_title
68+
@file_path = file_path
69+
@dirty = false
70+
refresh_title
5971
rescue ex
6072
window.msg_box_error("Error", "Could not open file: #{ex.message}")
6173
end
6274
end
6375
end
76+
end
6477

78+
private def bind_save(save_item)
6579
# Save file functionality
6680
save_item.on_clicked do |window|
67-
if @current_file_path
81+
if @file_path
6882
begin
69-
if current_file_path = @current_file_path
70-
File.write(current_file_path, @entry.text || "")
83+
if path = @file_path
84+
File.write(path, @entry.text || "")
7185
end
72-
@is_modified = false
73-
update_title
86+
@dirty = false
87+
refresh_title
7488
rescue ex
7589
window.msg_box_error("Error", "Could not save file: #{ex.message}")
7690
end
7791
else
7892
# If no current file, act like Save As
79-
if file_path = @main_window.save_file
93+
if file_path = @window.save_file
8094
begin
8195
File.write(file_path, @entry.text || "")
82-
@current_file_path = file_path
83-
@is_modified = false
84-
update_title
96+
@file_path = file_path
97+
@dirty = false
98+
refresh_title
8599
rescue ex
86-
@main_window.msg_box_error("Error", "Could not save file: #{ex.message}")
100+
@window.msg_box_error("Error", "Could not save file: #{ex.message}")
87101
end
88102
end
89103
end
90104
end
105+
end
91106

107+
private def bind_save_as(save_as_item)
92108
# Save As functionality
93109
save_as_item.on_clicked do |window|
94-
if file_path = @main_window.save_file
110+
if file_path = @window.save_file
95111
begin
96112
File.write(file_path, @entry.text || "")
97-
@current_file_path = file_path
98-
@is_modified = false
99-
update_title
113+
@file_path = file_path
114+
@dirty = false
115+
refresh_title
100116
rescue ex
101117
window.msg_box_error("Error", "Could not save file: #{ex.message}")
102118
end
103119
end
104120
end
121+
end
105122

123+
private def bind_quit
106124
# Quit functionality - use UIng.on_should_quit instead of quit_item.on_clicked
107125
UIng.on_should_quit do
108-
if @is_modified
126+
if @dirty
109127
# In a real application, you might want to show a confirmation dialog
110128
# For now, we'll just quit
111129
end
112130
cleanup
113131
true
114132
end
133+
end
115134

135+
private def bind_about(about_item)
116136
# About functionality
117137
about_item.on_clicked do |window|
118138
window.msg_box("About Notepad", "Simple Notepad Application\nBuilt with UIng (Crystal binding for libui-ng)")
119139
end
140+
end
120141

142+
private def bind_window_closing
121143
# Window closing handler
122-
@main_window.on_closing do
123-
if @is_modified
144+
@window.on_closing do
145+
if @dirty
124146
# In a real application, you might want to show a save confirmation dialog
125147
else
126148
end
@@ -129,20 +151,20 @@ class NotepadApp
129151
end
130152
end
131153

132-
def update_title
133-
title = if current_file_path = @current_file_path
134-
filename = File.basename(current_file_path)
135-
modified_marker = @is_modified ? "*" : ""
154+
def refresh_title
155+
title = if path = @file_path
156+
filename = File.basename(path)
157+
modified_marker = @dirty ? "*" : ""
136158
"#{modified_marker}#{filename} - Notepad"
137159
else
138-
modified_marker = @is_modified ? "*" : ""
160+
modified_marker = @dirty ? "*" : ""
139161
"#{modified_marker}Untitled - Notepad"
140162
end
141-
@main_window.title = title
163+
@window.title = title
142164
end
143165

144166
def run
145-
@main_window.show
167+
@window.show
146168
UIng.main
147169
UIng.uninit
148170
end
@@ -151,7 +173,7 @@ class NotepadApp
151173
# See https://github.com/kojix2/uing/issues/19
152174
@vbox.delete(0)
153175
@entry.destroy
154-
@main_window.destroy
176+
@window.destroy
155177
puts "Bye Bye"
156178
end
157179
end

0 commit comments

Comments
 (0)