bugfix: don't clear selection on M-arrow
[view.love.git] / run.lua
blob2cdd8926b52ef80c09648f5689773c97f856629b
1 run = {}
3 Editor_state = {}
5 -- called both in tests and real run
6 function run.initialize_globals()
7 -- tests currently mostly clear their own state
9 -- blinking cursor
10 Cursor_time = 0
11 end
13 -- called only for real run
14 function run.initialize(arg)
15 log_new('run')
16 if Settings then
17 run.load_settings()
18 else
19 run.initialize_default_settings()
20 end
22 if #arg > 0 and Editor_state.filename ~= absolutize(arg[1]) then
23 Editor_state.filename = arg[1]
24 load_from_disk(Editor_state)
25 Text.redraw_all(Editor_state)
26 Editor_state.screen_top1 = {line=1, pos=1}
27 Editor_state.cursor1 = {line=1, pos=1}
28 else
29 load_from_disk(Editor_state)
30 Text.redraw_all(Editor_state)
31 end
32 edit.check_locs(Editor_state)
36 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
37 love.window.setTitle('lines.love - '..Editor_state.filename)
41 if #arg > 1 then
42 print('ignoring commandline args after '..arg[1])
43 end
45 if rawget(_G, 'jit') then
46 jit.off()
47 jit.flush()
48 end
49 end
51 function print_and_log(s)
52 print(s)
53 log(3, s)
54 end
56 function run.load_settings()
57 local font = love.graphics.newFont(Settings.font_height)
58 -- set up desired window dimensions and make window resizable
59 _, _, App.screen.flags = App.screen.size()
60 App.screen.flags.resizable = true
61 App.screen.width, App.screen.height = Settings.width, Settings.height
62 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
63 run.set_window_position_from_settings(Settings)
64 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, Settings.font_height, math.floor(Settings.font_height*1.3))
65 Editor_state.filename = Settings.filename
66 Editor_state.screen_top1 = Settings.screen_top
67 Editor_state.cursor1 = Settings.cursor
68 end
70 function run.set_window_position_from_settings(settings)
71 local os = love.system.getOS()
72 if os == 'Linux' then
73 -- love.window.setPosition doesn't quite seem to do what is asked of it on Linux.
74 App.screen.move(settings.x, settings.y-37, settings.displayindex)
75 else
76 App.screen.move(settings.x, settings.y, settings.displayindex)
77 end
78 end
80 function run.initialize_default_settings()
81 local font_height = 20
82 local font = love.graphics.newFont(font_height)
83 run.initialize_window_geometry()
84 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, font_height, math.floor(font_height*1.3))
85 Settings = run.settings()
86 end
88 function run.initialize_window_geometry()
89 -- Initialize window width/height and make window resizable.
91 -- I get tempted to have opinions about window dimensions here, but they're
92 -- non-portable:
93 -- - maximizing doesn't work on mobile and messes things up
94 -- - maximizing keeps the title bar on screen in Linux, but off screen on
95 -- Windows. And there's no way to get the height of the title bar.
96 -- It seems more robust to just follow LÖVE's default window size until
97 -- someone overrides it.
98 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
99 App.screen.flags.resizable = true
100 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
103 function run.resize(w, h)
104 --? print(("Window resized to width: %d and height: %d."):format(w, h))
105 App.screen.width, App.screen.height = w, h
106 Text.redraw_all(Editor_state)
107 Editor_state.selection1 = {} -- no support for shift drag while we're resizing
108 Editor_state.right = App.screen.width-Margin_right
109 Editor_state.width = Editor_state.right-Editor_state.left
110 Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
113 function run.file_drop(file)
114 -- first make sure to save edits on any existing file
115 if Editor_state.next_save then
116 save_to_disk(Editor_state)
118 -- clear the slate for the new file
119 App.initialize_globals()
120 Editor_state.filename = file:getFilename()
121 file:open('r')
122 Editor_state.lines = load_from_file(file)
123 file:close()
124 Text.redraw_all(Editor_state)
125 Editor_state.screen_top1 = {line=1, pos=1}
126 Editor_state.cursor1 = {line=1, pos=1}
130 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
131 love.window.setTitle('lines.love - '..Editor_state.filename)
137 function run.draw()
138 edit.draw(Editor_state)
141 function run.update(dt)
142 Cursor_time = Cursor_time + dt
143 edit.update(Editor_state, dt)
146 function run.quit()
147 edit.quit(Editor_state)
150 function run.settings()
151 if Settings == nil then Settings = {} end
152 Settings.x, Settings.y, Settings.displayindex = App.screen.position()
153 return {
154 x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
155 width=App.screen.width, height=App.screen.height,
156 font_height=Editor_state.font_height,
157 filename=absolutize(Editor_state.filename),
158 screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
162 function absolutize(path)
163 if is_relative_path(path) then
164 return App.current_dir..path
166 return path
169 function run.mouse_press(x,y, mouse_button)
170 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
171 return edit.mouse_press(Editor_state, x,y, mouse_button)
174 function run.mouse_release(x,y, mouse_button)
175 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
176 return edit.mouse_release(Editor_state, x,y, mouse_button)
179 function run.mouse_wheel_move(dx,dy)
180 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
181 return edit.mouse_wheel_move(Editor_state, dx,dy)
184 function run.text_input(t)
185 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
186 return edit.text_input(Editor_state, t)
189 function run.keychord_press(chord, key)
190 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
191 return edit.keychord_press(Editor_state, chord, key)
194 function run.key_release(key, scancode)
195 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
196 return edit.key_release(Editor_state, key, scancode)
199 function width(s)
200 return love.graphics.getFont():getWidth(s)