Workspace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 (UI fits its size to the rows afterwards), below the
  53. // Playlist's toolbar row and to the right of its pattern picker and track names, so the arrangement
  54. // stays readable; on a narrow desktop it moves left to keep its usual width.
  55. const auto gap = 16;
  56. const auto rackWidth = std::min (w - browserWidth - 2 * gap, 720);
  57. const auto rackHeight = std::min (h - 2 * gap, std::max (240, h * 55 / 100));
  58. const auto rackTop = std::min (40, h / 10);
  59. const auto rackLeft = std::max (browserWidth + gap, std::min (browserWidth + playlistHeaderWidth, w - gap - rackWidth));
  60. l[Panel::channelRack] = state ({ rackLeft, rackTop, rackWidth, std::min (rackHeight, h - rackTop - gap) }, true);
  61. // Hidden until called (F7, F9), with rects that tile when both are shown on a tall enough desktop: the
  62. // Mixer along the bottom, the Piano Roll above it. On a short desktop the Piano Roll keeps a usable height
  63. // and overlaps the Mixer, as floating windows do.
  64. const auto mixerHeight = std::min (h - 2 * gap, std::max (mixerMinHeight, h * 45 / 100));
  65. const auto mixerTop = h - mixerHeight - gap / 2;
  66. l[Panel::mixer] = state ({ browserWidth + gap / 2, mixerTop, w - browserWidth - gap, mixerHeight }, false);
  67. const auto rollTop = 2 * gap;
  68. const auto tiledRoll = mixerTop - gap / 2 - rollTop;
  69. const auto rollHeight = tiledRoll >= pianoRollMinHeight ? tiledRoll : std::max (minHeight, h * 58 / 100);
  70. l[Panel::pianoRoll] = state ({ browserWidth + 2 * gap, rollTop, w - browserWidth - 4 * gap, rollHeight }, false);
  71. for (auto& p : l.panels)
  72. p.bounds = clampInto (p.bounds, w, h);
  73. l.order = { Panel::playlist, Panel::browser, Panel::mixer, Panel::pianoRoll, Panel::channelRack };
  74. l.focused = Panel::channelRack;
  75. return l;
  76. }
  77. void Layout::relayoutDefault (int newWidth, int newHeight)
  78. {
  79. const auto fresh = makeDefault (newWidth, newHeight);
  80. for (int i = 0; i < panelCount; ++i)
  81. panels[(std::size_t) i].bounds = fresh.panels[(std::size_t) i].bounds;
  82. width = fresh.width;
  83. height = fresh.height;
  84. }
  85. void Layout::bringToFront (Panel p)
  86. {
  87. order.erase (std::remove (order.begin(), order.end(), p), order.end());
  88. order.push_back (p);
  89. }
  90. bool Layout::isFrontmost (Panel p) const
  91. {
  92. for (auto it = order.rbegin(); it != order.rend(); ++it)
  93. if ((*this)[*it].visible && ! (*this)[*it].detached)
  94. return *it == p;
  95. return false;
  96. }
  97. void Layout::show (Panel p)
  98. {
  99. (*this)[p].visible = true;
  100. bringToFront (p);
  101. focused = p;
  102. }
  103. void Layout::hide (Panel p)
  104. {
  105. (*this)[p].visible = false;
  106. if (focused == p)
  107. for (auto it = order.rbegin(); it != order.rend(); ++it)
  108. if ((*this)[*it].visible)
  109. {
  110. focused = *it;
  111. break;
  112. }
  113. }
  114. bool Layout::toggle (Panel p)
  115. {
  116. auto& state = (*this)[p];
  117. if (state.visible && focused == p)
  118. {
  119. hide (p);
  120. return false;
  121. }
  122. show (p);
  123. return true;
  124. }
  125. void Layout::toggleMaximised (Panel p)
  126. {
  127. auto& state = (*this)[p];
  128. state.maximised = ! state.maximised;
  129. if (state.maximised)
  130. show (p);
  131. }
  132. void Layout::resize (int newWidth, int newHeight)
  133. {
  134. newWidth = std::max (1, newWidth);
  135. newHeight = std::max (1, newHeight);
  136. if (width > 0 && height > 0 && (newWidth != width || newHeight != height))
  137. {
  138. const auto sx = (double) newWidth / width, sy = (double) newHeight / height;
  139. for (auto& p : panels)
  140. {
  141. // edges that touched the desktop border keep touching it
  142. const auto r = p.bounds;
  143. auto x0 = (int) std::lround (r.x * sx), y0 = (int) std::lround (r.y * sy);
  144. auto x1 = r.right() >= width ? newWidth : (int) std::lround (r.right() * sx);
  145. auto y1 = r.bottom() >= height ? newHeight : (int) std::lround (r.bottom() * sy);
  146. p.bounds = { x0, y0, x1 - x0, y1 - y0 };
  147. }
  148. }
  149. width = newWidth;
  150. height = newHeight;
  151. for (auto& p : panels)
  152. p.bounds = clampInto (p.bounds, width, height);
  153. }
  154. int Layout::bringBackOffscreen()
  155. {
  156. int moved = 0;
  157. for (auto& p : panels)
  158. if (! isReachable (p.bounds, width, height))
  159. {
  160. p.bounds = clampInto (p.bounds, width, height);
  161. ++moved;
  162. }
  163. return moved;
  164. }
  165. std::string Layout::serialise() const
  166. {
  167. std::ostringstream out;
  168. out << "lynx-layout 1\n";
  169. char line[256];
  170. for (int i = 0; i < panelCount; ++i)
  171. {
  172. const auto& p = panels[(std::size_t) i];
  173. const auto fw = (double) std::max (1, width), fh = (double) std::max (1, height);
  174. 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,
  175. p.maximised ? 1 : 0, p.detached ? 1 : 0, p.bounds.x / fw, p.bounds.y / fh, p.bounds.w / fw, p.bounds.h / fh,
  176. p.windowBounds.x, p.windowBounds.y, p.windowBounds.w, p.windowBounds.h);
  177. out << line;
  178. }
  179. out << "order";
  180. for (auto p : order)
  181. out << ' ' << ids[(int) p];
  182. out << "\nfocus " << ids[(int) focused] << "\n";
  183. return out.str();
  184. }
  185. std::optional<Layout> Layout::parse (const std::string& text, int width, int height)
  186. {
  187. auto layout = makeDefault (width, height);
  188. std::istringstream in (text);
  189. std::string header;
  190. int version = 0;
  191. if (! (in >> header >> version) || header != "lynx-layout" || version != 1)
  192. return std::nullopt;
  193. std::string line;
  194. int panelsRead = 0;
  195. while (std::getline (in, line))
  196. {
  197. std::istringstream words (line);
  198. std::string kind;
  199. if (! (words >> kind))
  200. continue;
  201. if (kind == "panel")
  202. {
  203. std::string id;
  204. int visible = 0, maximised = 0, detached = 0;
  205. double x = 0, y = 0, w = 0, h = 0;
  206. Rect window;
  207. if (! (words >> id >> visible >> maximised >> detached >> x >> y >> w >> h >> window.x >> window.y >> window.w >> window.h))
  208. return std::nullopt;
  209. const auto panel = panelFromId (id);
  210. if (! panel || ! std::isfinite (x + y + w + h))
  211. return std::nullopt;
  212. auto& p = layout[*panel];
  213. p.visible = visible != 0;
  214. p.maximised = maximised != 0;
  215. p.detached = detached != 0;
  216. p.windowBounds = window;
  217. p.bounds = clampInto ({ (int) std::lround (x * layout.width), (int) std::lround (y * layout.height),
  218. (int) std::lround (w * layout.width), (int) std::lround (h * layout.height) },
  219. layout.width, layout.height);
  220. ++panelsRead;
  221. }
  222. else if (kind == "order")
  223. {
  224. std::vector<Panel> order;
  225. std::string id;
  226. while (words >> id)
  227. if (const auto p = panelFromId (id); p && std::find (order.begin(), order.end(), *p) == order.end())
  228. order.push_back (*p);
  229. for (int i = 0; i < panelCount; ++i) // any missing panel goes to the back
  230. if (std::find (order.begin(), order.end(), (Panel) i) == order.end())
  231. order.insert (order.begin(), (Panel) i);
  232. layout.order = order;
  233. }
  234. else if (kind == "focus")
  235. {
  236. std::string id;
  237. if (words >> id)
  238. if (const auto p = panelFromId (id))
  239. layout.focused = *p;
  240. }
  241. }
  242. if (panelsRead == 0)
  243. return std::nullopt;
  244. return layout;
  245. }
  246. Rect snap (Rect r, const std::vector<Rect>& others, int desktopWidth, int desktopHeight, int threshold, int edges)
  247. {
  248. std::vector<int> xs { 0, desktopWidth }, ys { 0, desktopHeight };
  249. for (const auto& o : others)
  250. {
  251. // only edges the moving rect could meet (overlapping on the other axis, give or take the threshold)
  252. const auto overlapsVertically = r.y < o.bottom() + threshold && r.bottom() > o.y - threshold;
  253. const auto overlapsHorizontally = r.x < o.right() + threshold && r.right() > o.x - threshold;
  254. if (overlapsVertically)
  255. {
  256. xs.push_back (o.x);
  257. xs.push_back (o.right());
  258. }
  259. if (overlapsHorizontally)
  260. {
  261. ys.push_back (o.y);
  262. ys.push_back (o.bottom());
  263. }
  264. }
  265. auto nearest = [threshold] (int value, const std::vector<int>& lines, int& best) {
  266. int bestDistance = threshold + 1;
  267. for (auto line : lines)
  268. if (std::abs (line - value) < bestDistance)
  269. {
  270. bestDistance = std::abs (line - value);
  271. best = line;
  272. }
  273. return bestDistance <= threshold ? bestDistance : -1;
  274. };
  275. if (edges == 0) // move: the closer of the two edges on each axis wins
  276. {
  277. int a = 0, b = 0;
  278. const auto da = nearest (r.x, xs, a), db = nearest (r.right(), xs, b);
  279. if (da >= 0 && (db < 0 || da <= db))
  280. r.x = a;
  281. else if (db >= 0)
  282. r.x = b - r.w;
  283. const auto ta = nearest (r.y, ys, a), tb = nearest (r.bottom(), ys, b);
  284. if (ta >= 0 && (tb < 0 || ta <= tb))
  285. r.y = a;
  286. else if (tb >= 0)
  287. r.y = b - r.h;
  288. return r;
  289. }
  290. int line = 0;
  291. if ((edges & left) && nearest (r.x, xs, line) >= 0)
  292. {
  293. r.w += r.x - line;
  294. r.x = line;
  295. }
  296. if ((edges & right) && nearest (r.right(), xs, line) >= 0)
  297. r.w = line - r.x;
  298. if ((edges & top) && nearest (r.y, ys, line) >= 0)
  299. {
  300. r.h += r.y - line;
  301. r.y = line;
  302. }
  303. if ((edges & bottom) && nearest (r.bottom(), ys, line) >= 0)
  304. r.h = line - r.y;
  305. return r;
  306. }
  307. } // namespace lynx::workspace