wait a little to flush disk before quitting
[view.love.git] / run.lua
blobec6a509335313e62017a975d4b54177136ead253
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 -- a few text objects we can avoid recomputing unless the font changes
10 Text_cache = {}
12 -- blinking cursor
13 Cursor_time = 0
14 end
16 -- called only for real run
17 function run.initialize(arg)
18 log_new('run')
19 if Settings then
20 run.load_settings()
21 else
22 run.initialize_default_settings()
23 end
25 if #arg > 0 then
26 Editor_state.filename = arg[1]
27 load_from_disk(Editor_state)
28 Text.redraw_all(Editor_state)
29 Editor_state.screen_top1 = {line=1, pos=1}
30 Editor_state.cursor1 = {line=1, pos=1}
31 edit.fixup_cursor(Editor_state)
32 else
33 load_from_disk(Editor_state)
34 Text.redraw_all(Editor_state)
35 if Editor_state.cursor1.line > #Editor_state.lines or Editor_state.lines[Editor_state.cursor1.line].mode ~= 'text' then
36 edit.fixup_cursor(Editor_state)
37 end
38 end
39 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 run.load_settings()
52 love.graphics.setFont(love.graphics.newFont(Settings.font_height))
53 -- determine default dimensions and flags
54 App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
55 -- set up desired window dimensions
56 App.screen.flags.resizable = true
57 App.screen.flags.minwidth = math.min(App.screen.width, 200)
58 App.screen.flags.minheight = math.min(App.screen.height, 200)
59 App.screen.width, App.screen.height = Settings.width, Settings.height
60 love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
61 love.window.setPosition(Settings.x, Settings.y, Settings.displayindex)
62 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3))
63 Editor_state.filename = Settings.filename
64 Editor_state.screen_top1 = Settings.screen_top
65 Editor_state.cursor1 = Settings.cursor
66 end
68 function run.initialize_default_settings()
69 local font_height = 20
70 love.graphics.setFont(love.graphics.newFont(font_height))
71 local em = App.newText(love.graphics.getFont(), 'm')
72 run.initialize_window_geometry(App.width(em))
73 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
74 Editor_state.font_height = font_height
75 Editor_state.line_height = math.floor(font_height*1.3)
76 Editor_state.em = em
77 Settings = run.settings()
78 end
80 function run.initialize_window_geometry(em_width)
81 -- maximize window
82 love.window.setMode(0, 0) -- maximize
83 App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
84 -- shrink height slightly to account for window decoration
85 App.screen.height = App.screen.height-100
86 App.screen.width = 40*em_width
87 App.screen.flags.resizable = true
88 App.screen.flags.minwidth = math.min(App.screen.width, 200)
89 App.screen.flags.minheight = math.min(App.screen.height, 200)
90 love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
91 end
93 function run.resize(w, h)
94 --? print(("Window resized to width: %d and height: %d."):format(w, h))
95 App.screen.width, App.screen.height = w, h
96 Text.redraw_all(Editor_state)
97 Editor_state.selection1 = {} -- no support for shift drag while we're resizing
98 Editor_state.right = App.screen.width-Margin_right
99 Editor_state.width = Editor_state.right-Editor_state.left
100 Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
103 function run.file_drop(file)
104 -- first make sure to save edits on any existing file
105 if Editor_state.next_save then
106 save_to_disk(Editor_state)
108 -- clear the slate for the new file
109 App.initialize_globals()
110 Editor_state.filename = file:getFilename()
111 file:open('r')
112 Editor_state.lines = load_from_file(file)
113 file:close()
114 Text.redraw_all(Editor_state)
115 edit.fixup_cursor(Editor_state)
116 love.window.setTitle('lines.love - '..Editor_state.filename)
119 function run.draw()
120 edit.draw(Editor_state)
123 function run.update(dt)
124 Cursor_time = Cursor_time + dt
125 edit.update(Editor_state, dt)
128 function run.quit()
129 edit.quit(Editor_state)
132 function run.settings()
133 if Settings == nil then
134 Settings = {}
136 if Current_app == 'run' then
137 Settings.x, Settings.y, Settings.displayindex = love.window.getPosition()
139 local filename = Editor_state.filename
140 if is_relative_path(filename) then
141 filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
143 return {
144 x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
145 width=App.screen.width, height=App.screen.height,
146 font_height=Editor_state.font_height,
147 filename=filename,
148 screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
152 function run.mouse_press(x,y, mouse_button)
153 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
154 return edit.mouse_press(Editor_state, x,y, mouse_button)
157 function run.mouse_release(x,y, mouse_button)
158 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
159 return edit.mouse_release(Editor_state, x,y, mouse_button)
162 function run.text_input(t)
163 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
164 return edit.text_input(Editor_state, t)
167 function run.keychord_press(chord, key)
168 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
169 return edit.keychord_press(Editor_state, chord, key)
172 function run.key_release(key, scancode)
173 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
174 return edit.key_release(Editor_state, key, scancode)
177 -- use this sparingly
178 function to_text(s)
179 if Text_cache[s] == nil then
180 Text_cache[s] = App.newText(love.graphics.getFont(), s)
182 return Text_cache[s]