Mercurial Hosting > editor
annotate src/luan_editor/menu.luan @ 77:b4ed3c726b4c
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 19 Jun 2025 12:02:05 -0600 |
| parents | c3b0fba5c2bc |
| children | aefa7f8bb407 |
| rev | line source |
|---|---|
| 37 | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | |
| 40 | 3 local ipairs = Luan.ipairs or error() |
| 37 | 4 local String = require "luan:String.luan" |
| 5 local to_number = String.to_number or error() | |
| 6 local new_menu_item = require("luan:swing/Menu_item.luan").new or error() | |
| 7 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error() | |
| 8 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error() | |
| 9 local Menu = require "luan:swing/Menu.luan" | |
| 10 local new_menu = Menu.new or error() | |
| 11 local separator = Menu.separator or error() | |
| 12 local Option_pane = require "luan:swing/Option_pane.luan" | |
| 13 local show_message_dialog = Option_pane.show_message_dialog or error() | |
| 14 local show_input_dialog = Option_pane.show_input_dialog or error() | |
| 40 | 15 local Frame = require "luan:swing/Frame.luan" |
| 16 local get_all_frames = Frame.get_all_frames or error() | |
| 37 | 17 local Spell_checker = require "classpath:luan_editor/Spell_checker.luan" |
| 18 local spell_check = Spell_checker.spell_check or error() | |
| 56 | 19 local Window = require "classpath:luan_editor/Window.luan" |
| 20 local show_list_window = Window.show_list_window or error() | |
| 64 | 21 local new_window = Window.new_window or error() |
| 37 | 22 |
| 23 | |
| 24 local function action_listener(fn) | |
| 25 return function(_) | |
| 26 fn() | |
| 27 end | |
| 28 end | |
| 29 | |
| 30 local function add_menu_bar(window) | |
| 31 local text_area = window.text_area | |
| 32 local document = text_area.document | |
| 33 local status_bar = window.status_bar | |
| 34 local revert = new_menu_item{ | |
| 35 text = "Revert" | |
| 36 enabled = window.has_file | |
| 37 action_listener = action_listener(window.revert) | |
| 38 } | |
| 48 | 39 local view_file_path = new_menu_item{ |
| 40 text = "File Path" | |
| 41 enabled = window.has_file | |
| 42 action_listener = function(_) | |
| 43 status_bar.text = window.title() | |
| 44 end | |
| 45 } | |
| 37 | 46 local undo = new_menu_item{ |
| 47 text = "Undo" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
48 accelerator = "Z" |
| 37 | 49 action_listener = action_listener(document.undo) |
| 50 } | |
| 51 local redo = new_menu_item{ | |
| 52 text = "Redo" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
53 accelerator = "shift Z" |
| 37 | 54 action_listener = action_listener(document.redo) |
| 55 } | |
| 56 local function update_undo_redo() | |
| 57 undo.set_enabled(document.can_undo()) | |
| 58 redo.set_enabled(document.can_redo()) | |
| 59 end | |
| 60 window.update_undo_redo = update_undo_redo -- dont gc | |
| 61 update_undo_redo() | |
| 62 document.add_undo_listener(update_undo_redo) | |
| 63 | |
| 64 local menu_bar = new_menu_bar{ | |
| 65 menus = { | |
| 66 new_menu{ | |
| 67 text = "File" | |
| 68 menu_items = { | |
| 69 new_menu_item{ | |
| 70 text = "New File" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
71 accelerator = "N" |
| 64 | 72 action_listener = action_listener(new_window) |
| 37 | 73 } |
| 74 new_menu_item{ | |
| 75 text = "Open..." | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
76 accelerator = "O" |
| 37 | 77 action_listener = action_listener(window.open) |
| 78 } | |
| 79 new_menu_item{ | |
| 80 text = "Save" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
81 accelerator = "S" |
| 37 | 82 action_listener = function(_) |
| 83 if window.save() then | |
| 84 revert.set_enabled(true) | |
| 48 | 85 view_file_path.set_enabled(true) |
| 37 | 86 end |
| 87 end | |
| 88 } | |
| 89 new_menu_item{ | |
| 90 text = "Print" | |
| 91 action_listener = action_listener(text_area.print) | |
| 92 } | |
| 93 revert | |
| 55 | 94 new_menu_item{ |
| 74 | 95 text = "Paste Files" |
| 64 | 96 accelerator = "P" |
| 55 | 97 action_listener = function(_) |
| 98 if not window.paste_files() then | |
| 99 show_message_dialog(nil,"No files to paste") | |
| 100 end | |
| 101 end | |
| 102 } | |
| 37 | 103 --[[ |
| 104 new_menu_item{ | |
| 105 text = "Test" | |
| 106 action_listener = function(_) | |
| 107 local location = window.frame.location | |
| 108 location.y = location.y - 20 | |
| 109 window.frame.location = location | |
| 110 end | |
| 111 } | |
| 112 ]] | |
| 113 } | |
| 114 } | |
| 115 new_menu{ | |
| 116 text = "Edit" | |
| 117 menu_items = { | |
| 118 undo | |
| 119 redo | |
| 120 separator | |
| 121 new_menu_item{ | |
| 122 text = "Cut" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
123 accelerator = "X" |
| 37 | 124 action_listener = action_listener(text_area.cut) |
| 125 } | |
| 126 new_menu_item{ | |
| 127 text = "Copy" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
128 accelerator = "C" |
| 37 | 129 action_listener = action_listener(text_area.copy) |
| 130 } | |
| 131 new_menu_item{ | |
| 132 text = "Paste" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
133 accelerator = "V" |
| 37 | 134 action_listener = action_listener(text_area.paste) |
| 135 } | |
| 136 separator | |
| 137 new_menu_item{ | |
| 138 text = "Indent" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
139 accelerator = "CLOSE_BRACKET" |
| 37 | 140 action_listener = action_listener(window.indent) |
| 141 } | |
| 142 new_menu_item{ | |
| 143 text = "Unindent" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
144 accelerator = "OPEN_BRACKET" |
| 37 | 145 action_listener = action_listener(window.unindent) |
| 146 } | |
| 147 separator | |
| 148 new_menu_item{ | |
| 149 text = "Select All" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
150 accelerator = "A" |
| 37 | 151 action_listener = action_listener(text_area.select_all) |
| 152 } | |
| 153 } | |
| 154 } | |
| 155 new_menu{ | |
| 156 text = "Find" | |
| 157 menu_items = { | |
| 70 | 158 new_menu_item{ |
| 159 text = "Find and Replace" | |
| 160 accelerator = "F" | |
| 161 action_listener = action_listener(window.show_find_panel) | |
| 162 } | |
| 163 new_menu_item{ | |
| 164 text = "Close Find and Replace" | |
| 165 accelerator = "shift F" | |
| 166 action_listener = action_listener(window.hide_find_panel) | |
| 167 } | |
| 37 | 168 new_menu_item{ |
| 169 text = "Find Case Insensitive" | |
| 170 action_listener = window.find_case_insensitive | |
| 171 } | |
| 172 new_menu_item{ | |
| 173 text = "Convert Leading Tabs to Spaces" | |
| 174 action_listener = window.tabs_to_spaces | |
| 175 } | |
| 176 new_menu_item{ | |
| 177 text = "Convert Leading Spaces to Tabs" | |
| 178 action_listener = window.spaces_to_tabs | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 new_menu{ | |
| 183 text = "View" | |
| 184 menu_items = { | |
| 185 new_check_box_menu_item{ | |
| 186 text = "Word Wrap" | |
| 187 state = text_area.line_wrap | |
| 188 action_listener = function(event) | |
| 189 window.set_line_wrap(event.source.state) | |
| 190 end | |
| 191 } | |
| 192 new_check_box_menu_item{ | |
| 193 text = "Show Whitespace" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
194 accelerator = "W" |
| 37 | 195 state = text_area.whitespace_visible |
| 196 action_listener = function(event) | |
| 197 window.set_whitespace_visible(event.source.state) | |
| 198 end | |
| 199 } | |
| 200 new_check_box_menu_item{ | |
| 201 text = "Spell Check" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
202 accelerator = "SEMICOLON" |
| 37 | 203 action_listener = function(event) |
| 204 spell_check(text_area,event.source.state) | |
| 205 end | |
| 206 } | |
| 207 new_menu_item{ | |
| 208 text = "Cursor Column" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
209 accelerator = "B" |
| 37 | 210 action_listener = function(_) |
| 211 status_bar.text = "Cursor Column: "..window.cursor_column() | |
| 212 end | |
| 213 } | |
| 214 new_menu_item{ | |
| 215 text = "Goto Line" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
216 accelerator = "G" |
| 37 | 217 action_listener = function(_) |
| 218 local input = show_input_dialog( window.frame, "Goto line" ) | |
| 219 if input == nil then | |
| 220 return | |
| 221 end | |
| 222 local line = to_number(input) | |
| 223 try | |
| 224 window.goto(line) | |
| 225 status_bar.text = "Went to line "..line | |
| 226 catch e | |
| 227 status_bar.text = "Invalid line: "..input | |
| 228 end | |
| 229 end | |
| 230 } | |
| 231 new_menu_item{ | |
| 232 text = "Tab Size" | |
| 233 action_listener = function(_) | |
| 234 local input = show_input_dialog( window.frame, "Tab size", text_area.tab_size ) | |
| 235 if input == nil then | |
| 236 return | |
| 237 end | |
| 238 local size = to_number(input) | |
| 239 try | |
| 240 window.set_tab_size(size) | |
| 241 status_bar.text = "Set tab size to "..size | |
| 242 catch e | |
| 243 status_bar.text = "Invalid tab size: "..input | |
| 244 end | |
| 245 end | |
| 246 } | |
| 48 | 247 view_file_path |
| 37 | 248 } |
| 249 } | |
| 38 | 250 new_menu{ |
| 251 text = "Window" | |
| 252 menu_items = { | |
| 253 new_menu_item{ | |
| 254 text = "Duplicate Window" | |
| 255 action_listener = action_listener(window.duplicate) | |
| 256 } | |
| 40 | 257 new_menu_item{ |
| 258 text = "Align Windows" | |
| 259 action_listener = function(_) | |
| 260 local this_frame = window.frame | |
| 261 local location = this_frame.location | |
| 262 local size = this_frame.size | |
| 263 for _, frame in ipairs(get_all_frames()) do | |
| 264 frame.location = location | |
| 265 frame.size = size | |
| 266 end | |
| 267 end | |
| 268 } | |
| 41 | 269 new_menu_item{ |
| 270 text = "List Windows" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
271 accelerator = "L" |
| 56 | 272 action_listener = action_listener(show_list_window) |
| 41 | 273 } |
| 38 | 274 } |
| 275 } | |
| 37 | 276 } |
| 277 } | |
| 278 window.frame.set_menu_bar(menu_bar) | |
| 279 end | |
| 280 | |
| 281 return add_menu_bar |
