Workspace.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "core/Workspace.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <sstream>
  6. namespace lynx::workspace
  7. {
  8. namespace
  9. {
  10. const char* const ids[panelCount] = { "playlist", "channelRack", "pianoRoll", "mixer", "browser" };
  11. Rect clampInto (Rect r, int width, int height)
  12. {
  13. r.w = std::clamp (r.w, std::min (minWidth, std::max (1, width)), std::max (1, width));
  14. r.h = std::clamp (r.h, std::min (minHeight, std::max (1, height)), std::max (1, height));
  15. r.x = std::clamp (r.x, 0, std::max (0, width - r.w));
  16. r.y = std::clamp (r.y, 0, std::max (0, height - r.h));
  17. return r;
  18. }
  19. // a panel counts as off-screen when less than this much of its title strip is reachable
  20. bool isReachable (const Rect& r, int width, int height)
  21. {
  22. constexpr int grab = 40;
  23. return r.right() >= grab && r.x <= width - grab && r.y >= 0 && r.y <= height - 20 && r.w > 0 && r.h > 0;
  24. }
  25. } // namespace
  26. const char* panelId (Panel panel) noexcept
  27. {
  28. return ids[(int) panel];
  29. }
  30. std::optional<Panel> panelFromId (const std::string& id)
  31. {
  32. for (int i = 0; i < panelCount; ++i)
  33. if (id == ids[i])
  34. return (Panel) i;
  35. return std::nullopt;
  36. }
  37. Layout Layout::makeDefault (int width, int height)
  38. {
  39. Layout l;
  40. l.width = std::max (1, width);
  41. l.height = std::max (1, height);
  42. const auto w = l.width, h = l.height;
  43. auto state = [] (Rect r, bool visible) {
  44. PanelState p;
  45. p.bounds = r;
  46. p.visible = visible;
  47. return p;
  48. };
  49. const auto browserWidth = std::clamp (w * 17 / 100, minWidth, 260);
  50. l[Panel::browser] = state ({ 0, 0, browserWidth, h }, true);
  51. l[Panel::playlist] = state ({ browserWidth, 0, w - browserWidth, h }, true);
  52. // The Channel Rack floats over the Playlist (FL's View › Arrange windows › Desktop (default): the Browser at the
  53. // left, the Playlist filling the rest, the rack a window above it; UI fits its size to the rows afterwards),
  54. // below the Playlist's toolbar row and to the right of its pattern picker and track names, so the arrangement
  55. // stays readable. On a small desktop (a laptop at 125/150 % scaling) it gets narrower (its steps scroll) rather
  56. // than covering the track names and the whole arrangement (round 167v); only when even rackMinWidth does not fit
  57. // there does it move left over the track names.
  58. const auto gap = 16;
  59. const auto besideHeaders = w - gap - (browserWidth + playlistHeaderWidth);
  60. const auto rackWidth = besideHeaders >= rackMinWidth ? std::min (besideHeaders, rackMaxWidth)
  61. : std::min (w - browserWidth - 2 * gap, rackMinWidth);
  62. const auto rackHeight = std::min (h - 2 * gap, std::max (240, h * 55 / 100));
  63. const auto rackTop = std::min (40, h / 10);
  64. const auto rackLeft = std::max (browserWidth + gap, std::min (browserWidth + playlistHeaderWidth, w - gap - rackWidth));
  65. l[Panel::channelRack] = state ({ rackLeft, rackTop, rackWidth, std::min (rackHeight, h - rackTop - gap) }, true);
  66. // Hidden until called (F7, F9), with rects that tile when both are shown on a tall enough desktop: the
  67. // Mixer along the bottom, the Piano Roll above it. On a short desktop the Piano Roll keeps a usable height
  68. // and overlaps the Mixer, as floating windows do.
  69. const auto mixerHeight = std::min (h - 2 * gap, std::max (mixerMinHeight, h * 45 / 100));
  70. const auto mixerTop = h - mixerHeight - gap / 2;
  71. l[Panel::mixer] = state ({ browserWidth + gap / 2, mixerTop, w - browserWidth - gap, mixerHeight }, false);
  72. const auto rollTop = 2 * gap;
  73. const auto tiledRoll = mixerTop - gap / 2 - rollTop;
  74. const auto rollHeight = tiledRoll >= pianoRollMinHeight ? tiledRoll : std::max (minHeight, h * 58 / 100);
  75. l[Panel::pianoRoll] = state ({ browserWidth + 2 * gap, rollTop, w - browserWidth - 4 * gap, rollHeight }, false);
  76. for (auto& p : l.panels)
  77. p.bounds = clampInto (p.bounds, w, h);
  78. l.order = { Panel::playlist, Panel::browser, Panel::mixer, Panel::pianoRoll, Panel::channelRack };
  79. l.focused = Panel::channelRack;
  80. return l;
  81. }
  82. void Layout::relayoutDefault (int newWidth, int newHeight)
  83. {
  84. const auto fresh = makeDefault (newWidth, newHeight);
  85. for (int i = 0; i < panelCount; ++i)
  86. panels[(std::size_t) i].bounds = fresh.panels[(std::size_t) i].bounds;
  87. width = fresh.width;
  88. height = fresh.height;
  89. }
  90. void Layout::bringToFront (Panel p)
  91. {
  92. order.erase (std::remove (order.begin(), order.end(), p), order.end());
  93. order.push_back (p);
  94. }
  95. bool Layout::isFrontmost (Panel p) const
  96. {
  97. for (auto it = order.rbegin(); it != order.rend(); ++it)
  98. if ((*this)[*it].visible && ! (*this)[*it].detached)
  99. return *it == p;
  100. return false;
  101. }
  102. std::vector<Panel> Layout::stacking() const
  103. {
  104. std::vector<Panel> out;
  105. for (auto background : { true, false })
  106. for (auto p : order)
  107. if (! (*this)[p].detached && isBackground (p) == background)
  108. out.push_back (p);
  109. return out;
  110. }
  111. void Layout::show (Panel p)
  112. {
  113. (*this)[p].visible = true;
  114. bringToFront (p);
  115. focused = p;
  116. }
  117. void Layout::hide (Panel p)
  118. {
  119. (*this)[p].visible = false;
  120. if (focused == p)
  121. for (auto it = order.rbegin(); it != order.rend(); ++it)
  122. if ((*this)[*it].visible)
  123. {
  124. focused = *it;
  125. break;
  126. }
  127. }
  128. bool Layout::toggle (Panel p)
  129. {
  130. auto& state = (*this)[p];
  131. if (state.visible && focused == p)
  132. {
  133. hide (p);
  134. return false;
  135. }
  136. show (p);
  137. return true;
  138. }
  139. void Layout::toggleMaximised (Panel p)
  140. {
  141. auto& state = (*this)[p];
  142. state.maximised = ! state.maximised;
  143. if (state.maximised)
  144. show (p);
  145. }
  146. void Layout::resize (int newWidth, int newHeight)
  147. {
  148. newWidth = std::max (1, newWidth);
  149. newHeight = std::max (1, newHeight);
  150. const auto dockedPlaylist = width > 0 && browserDocksPlaylist();
  151. if (width > 0 && height > 0 && (newWidth != width || newHeight != height))
  152. {
  153. const auto sx = (double) newWidth / width, sy = (double) newHeight / height;
  154. for (auto& p : panels)
  155. {
  156. // edges that touched the desktop border keep touching it
  157. const auto r = p.bounds;
  158. auto x0 = (int) std::lround (r.x * sx), y0 = (int) std::lround (r.y * sy);
  159. auto x1 = r.right() >= width ? newWidth : (int) std::lround (r.right() * sx);
  160. auto y1 = r.bottom() >= height ? newHeight : (int) std::lround (r.bottom() * sy);
  161. p.bounds = { x0, y0, x1 - x0, y1 - y0 };
  162. }
  163. }
  164. width = newWidth;
  165. height = newHeight;
  166. for (auto& p : panels)
  167. p.bounds = clampInto (p.bounds, width, height);
  168. if (dockedPlaylist)
  169. redockPlaylist();
  170. }
  171. // The Browser at the desktop's left edge with the Playlist starting where it ends (the default arrangement):
  172. // they stay side by side however the desktop is resized (the Browser's minimum width must not push it over the
  173. // Playlist's pattern picker, round 167v).
  174. bool Layout::browserDocksPlaylist() const
  175. {
  176. const auto& b = (*this)[Panel::browser].bounds;
  177. const auto& p = (*this)[Panel::playlist].bounds;
  178. return b.x == 0 && p.x == b.right() && p.right() >= width;
  179. }
  180. void Layout::redockPlaylist()
  181. {
  182. const auto& b = (*this)[Panel::browser].bounds;
  183. auto& p = (*this)[Panel::playlist].bounds;
  184. if (b.x != 0 || b.right() > width - minWidth)
  185. return;
  186. p.x = b.right();
  187. p.w = width - p.x;
  188. }
  189. int Layout::bringBackOffscreen()
  190. {
  191. int moved = 0;
  192. for (auto& p : panels)
  193. if (! isReachable (p.bounds, width, height))
  194. {
  195. p.bounds = clampInto (p.bounds, width, height);
  196. ++moved;
  197. }
  198. return moved;
  199. }
  200. std::string Layout::serialise() const
  201. {
  202. std::ostringstream out;
  203. out << "lynx-layout 1\n";
  204. char line[256];
  205. for (int i = 0; i < panelCount; ++i)
  206. {
  207. const auto& p = panels[(std::size_t) i];
  208. const auto fw = (double) std::max (1, width), fh = (double) std::max (1, height);
  209. std::snprintf (line, sizeof (line), "panel %s %d %d %d %.5f %.5f %.5f %.5f %d %d %d %d\n", ids[i], p.visible ? 1 : 0,
  210. p.maximised ? 1 : 0, p.detached ? 1 : 0, p.bounds.x / fw, p.bounds.y / fh, p.bounds.w / fw, p.bounds.h / fh,
  211. p.windowBounds.x, p.windowBounds.y, p.windowBounds.w, p.windowBounds.h);
  212. out << line;
  213. }
  214. out << "order";
  215. for (auto p : order)
  216. out << ' ' << ids[(int) p];
  217. out << "\nfocus " << ids[(int) focused] << "\n";
  218. return out.str();
  219. }
  220. std::optional<Layout> Layout::parse (const std::string& text, int width, int height)
  221. {
  222. auto layout = makeDefault (width, height);
  223. std::istringstream in (text);
  224. std::string header;
  225. int version = 0;
  226. if (! (in >> header >> version) || header != "lynx-layout" || version != 1)
  227. return std::nullopt;
  228. std::string line;
  229. int panelsRead = 0;
  230. while (std::getline (in, line))
  231. {
  232. std::istringstream words (line);
  233. std::string kind;
  234. if (! (words >> kind))
  235. continue;
  236. if (kind == "panel")
  237. {
  238. std::string id;
  239. int visible = 0, maximised = 0, detached = 0;
  240. double x = 0, y = 0, w = 0, h = 0;
  241. Rect window;
  242. if (! (words >> id >> visible >> maximised >> detached >> x >> y >> w >> h >> window.x >> window.y >> window.w >> window.h))
  243. return std::nullopt;
  244. const auto panel = panelFromId (id);
  245. if (! panel || ! std::isfinite (x + y + w + h))
  246. return std::nullopt;
  247. auto& p = layout[*panel];
  248. p.visible = visible != 0;
  249. p.maximised = maximised != 0;
  250. p.detached = detached != 0;
  251. p.windowBounds = window;
  252. p.bounds = clampInto ({ (int) std::lround (x * layout.width), (int) std::lround (y * layout.height),
  253. (int) std::lround (w * layout.width), (int) std::lround (h * layout.height) },
  254. layout.width, layout.height);
  255. ++panelsRead;
  256. }
  257. else if (kind == "order")
  258. {
  259. std::vector<Panel> order;
  260. std::string id;
  261. while (words >> id)
  262. if (const auto p = panelFromId (id); p && std::find (order.begin(), order.end(), *p) == order.end())
  263. order.push_back (*p);
  264. for (int i = 0; i < panelCount; ++i) // any missing panel goes to the back
  265. if (std::find (order.begin(), order.end(), (Panel) i) == order.end())
  266. order.insert (order.begin(), (Panel) i);
  267. layout.order = order;
  268. }
  269. else if (kind == "focus")
  270. {
  271. std::string id;
  272. if (words >> id)
  273. if (const auto p = panelFromId (id))
  274. layout.focused = *p;
  275. }
  276. }
  277. if (panelsRead == 0)
  278. return std::nullopt;
  279. // A layout saved by 0.1.21 or earlier could have the Browser (at the left edge) wider than where the Playlist
  280. // starts, covering its pattern picker (a minimum width applied while the window was still small, then scaled):
  281. // the Playlist starts where the Browser ends again.
  282. {
  283. const auto& b = layout[Panel::browser].bounds;
  284. const auto& p = layout[Panel::playlist].bounds;
  285. if (b.x == 0 && p.x > 0 && p.x < b.right() && p.right() >= layout.width)
  286. layout.redockPlaylist();
  287. }
  288. return layout;
  289. }
  290. Rect snap (Rect r, const std::vector<Rect>& others, int desktopWidth, int desktopHeight, int threshold, int edges)
  291. {
  292. std::vector<int> xs { 0, desktopWidth }, ys { 0, desktopHeight };
  293. for (const auto& o : others)
  294. {
  295. // only edges the moving rect could meet (overlapping on the other axis, give or take the threshold)
  296. const auto overlapsVertically = r.y < o.bottom() + threshold && r.bottom() > o.y - threshold;
  297. const auto overlapsHorizontally = r.x < o.right() + threshold && r.right() > o.x - threshold;
  298. if (overlapsVertically)
  299. {
  300. xs.push_back (o.x);
  301. xs.push_back (o.right());
  302. }
  303. if (overlapsHorizontally)
  304. {
  305. ys.push_back (o.y);
  306. ys.push_back (o.bottom());
  307. }
  308. }
  309. auto nearest = [threshold] (int value, const std::vector<int>& lines, int& best) {
  310. int bestDistance = threshold + 1;
  311. for (auto line : lines)
  312. if (std::abs (line - value) < bestDistance)
  313. {
  314. bestDistance = std::abs (line - value);
  315. best = line;
  316. }
  317. return bestDistance <= threshold ? bestDistance : -1;
  318. };
  319. if (edges == 0) // move: the closer of the two edges on each axis wins
  320. {
  321. int a = 0, b = 0;
  322. const auto da = nearest (r.x, xs, a), db = nearest (r.right(), xs, b);
  323. if (da >= 0 && (db < 0 || da <= db))
  324. r.x = a;
  325. else if (db >= 0)
  326. r.x = b - r.w;
  327. const auto ta = nearest (r.y, ys, a), tb = nearest (r.bottom(), ys, b);
  328. if (ta >= 0 && (tb < 0 || ta <= tb))
  329. r.y = a;
  330. else if (tb >= 0)
  331. r.y = b - r.h;
  332. return r;
  333. }
  334. int line = 0;
  335. if ((edges & left) && nearest (r.x, xs, line) >= 0)
  336. {
  337. r.w += r.x - line;
  338. r.x = line;
  339. }
  340. if ((edges & right) && nearest (r.right(), xs, line) >= 0)
  341. r.w = line - r.x;
  342. if ((edges & top) && nearest (r.y, ys, line) >= 0)
  343. {
  344. r.h += r.y - line;
  345. r.y = line;
  346. }
  347. if ((edges & bottom) && nearest (r.bottom(), ys, line) >= 0)
  348. r.h = line - r.y;
  349. return r;
  350. }
  351. } // namespace lynx::workspace