Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
7d2bf5996f
commit
2fa7bd745b
150 files changed
+8319
-462
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
|
||||
#include "layout.h"
|
||||
|
||||
#include <QBasicTimer>
|
||||
#include <QHash>
|
||||
#include <QImage>
|
||||
#include <QPointF>
|
||||
#include <QRasterWindow>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
struct AppInfo {
|
||||
QString label;
|
||||
QString component;
|
||||
bool system = false;
|
||||
};
|
||||
|
||||
struct WidgetProviderInfo {
|
||||
QString provider;
|
||||
QString label;
|
||||
int w = 2;
|
||||
int h = 2;
|
||||
};
|
||||
|
||||
class LauncherWindow : public QRasterWindow {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
LauncherWindow();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseMoveEvent(QMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
void wheelEvent(QWheelEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
enum class SheetKind { None, Menu, HomeItem, DrawerApp, Widgets };
|
||||
|
||||
void pollHostEvents();
|
||||
void refreshInsets();
|
||||
void refreshApps();
|
||||
void refreshWidgetProviders();
|
||||
void loadHome();
|
||||
void seedDefaultHome();
|
||||
void saveHome() const;
|
||||
QImage iconFor(const QString &component, int px);
|
||||
|
||||
// Geometry helpers (logical px)
|
||||
qreal insetTop() const;
|
||||
qreal insetBottom() const;
|
||||
qreal contentW() const;
|
||||
qreal contentX() const;
|
||||
qreal gridX() const;
|
||||
qreal gridW() const;
|
||||
int homeCols() const;
|
||||
qreal homeColW() const;
|
||||
qreal panelY() const;
|
||||
qreal gridTop() const;
|
||||
QRectF appsPillRect() const;
|
||||
QRectF drawerSearchRect() const;
|
||||
QRectF drawerCloseRect() const;
|
||||
QRectF drawerClearRect() const;
|
||||
QVector<AppInfo> filteredApps() const;
|
||||
int hitHomeSlot(const QPointF &pos) const;
|
||||
int hitDrawerApp(const QPointF &pos) const;
|
||||
int hitSheetButton(const QPointF &pos) const;
|
||||
|
||||
void openDrawer();
|
||||
void closeDrawer();
|
||||
void openSheet(SheetKind kind, int index);
|
||||
void closeSheet();
|
||||
void triggerSheetAction(int actionIdx);
|
||||
|
||||
void drawHome(QPainter &p);
|
||||
void drawDrawer(QPainter &p);
|
||||
void drawSheet(QPainter &p);
|
||||
void drawIconAndLabel(QPainter &p, const QRectF &box, const QString &component,
|
||||
const QString &label, int iconSize, bool lifted);
|
||||
|
||||
// Host state
|
||||
int m_physInsetTop = 0;
|
||||
int m_physInsetBottom = 0;
|
||||
QVector<AppInfo> m_apps;
|
||||
QHash<QString, int> m_appIndex;
|
||||
QVector<Layout::HomeSlot> m_home;
|
||||
QVector<WidgetProviderInfo> m_providers;
|
||||
QHash<QString, QImage> m_icons;
|
||||
QSet<QString> m_missingIcons;
|
||||
QString m_lastPlacedWidgets;
|
||||
bool m_lastWidgetsVisible = true;
|
||||
|
||||
// UI state
|
||||
bool m_drawerOpen = false;
|
||||
qreal m_drawerScroll = 0.0;
|
||||
qreal m_homeScroll = 0.0;
|
||||
QString m_search;
|
||||
bool m_searchFocused = false;
|
||||
SheetKind m_sheet = SheetKind::None;
|
||||
int m_sheetIndex = -1;
|
||||
|
||||
// Pointer / gesture tracking
|
||||
bool m_pressed = false;
|
||||
bool m_dragging = false;
|
||||
bool m_longPressed = false;
|
||||
int m_pressedHome = -1;
|
||||
int m_pressedDrawer = -1;
|
||||
int m_liftedHome = -1;
|
||||
QPointF m_pressPos;
|
||||
QPointF m_lastPos;
|
||||
QPointF m_liftPos;
|
||||
QElapsedTimer m_pressTimer;
|
||||
QBasicTimer m_pollTimer;
|
||||
QBasicTimer m_longPressTimer;
|
||||
};
|
||||
@@ -0,0 +1,87 @@
|
||||
#include "layout.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace Layout {
|
||||
|
||||
PackedGrid packHome(const QVector<HomeSlot> &items, int cols, const QSet<QString> &hidden) {
|
||||
PackedGrid out;
|
||||
out.cells.resize(items.size(), {-1, -1});
|
||||
if (cols <= 0) return out;
|
||||
|
||||
// Occupancy grid grows dynamically as rows are needed.
|
||||
std::vector<std::vector<bool>> occ;
|
||||
auto ensureRow = [&](int r) {
|
||||
while (static_cast<int>(occ.size()) <= r) occ.emplace_back(cols, false);
|
||||
};
|
||||
auto isFree = [&](int c, int r, int w, int h) -> bool {
|
||||
if (c < 0 || c + w > cols || r < 0) return false;
|
||||
for (int dr = 0; dr < h; ++dr) {
|
||||
const int rr = r + dr;
|
||||
if (rr >= static_cast<int>(occ.size())) continue;
|
||||
for (int dc = 0; dc < w; ++dc) {
|
||||
if (occ[rr][c + dc]) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
auto occupy = [&](int c, int r, int w, int h) {
|
||||
for (int dr = 0; dr < h; ++dr) {
|
||||
const int rr = r + dr;
|
||||
ensureRow(rr);
|
||||
for (int dc = 0; dc < w; ++dc) {
|
||||
occ[rr][c + dc] = true;
|
||||
}
|
||||
out.rows = std::max(out.rows, rr + 1);
|
||||
}
|
||||
};
|
||||
|
||||
// Pass 1: place pinned slots first (widgets before apps) if their area is free.
|
||||
QVector<bool> placed(items.size(), false);
|
||||
for (int pass = 0; pass < 2; ++pass) {
|
||||
const bool wantWidget = (pass == 0);
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
const auto &s = items[i];
|
||||
if (s.isWidget() != wantWidget) continue;
|
||||
if (!s.isWidget() && hidden.contains(s.component)) continue;
|
||||
const int w = std::clamp(s.w, 1, cols);
|
||||
const int h = std::max(1, s.h);
|
||||
if (s.x >= 0 && s.y >= 0 && isFree(s.x, s.y, w, h)) {
|
||||
occupy(s.x, s.y, w, h);
|
||||
out.cells[i] = {s.x, s.y};
|
||||
placed[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: pack remaining visible slots into the earliest free area.
|
||||
for (int i = 0; i < items.size(); ++i) {
|
||||
const auto &s = items[i];
|
||||
if (placed[i] || (!s.isWidget() && hidden.contains(s.component))) continue;
|
||||
const int w = std::clamp(s.w, 1, cols);
|
||||
const int h = std::max(1, s.h);
|
||||
int r = 0, c = 0;
|
||||
while (!isFree(c, r, w, h)) {
|
||||
++c;
|
||||
if (c + w > cols) {
|
||||
c = 0;
|
||||
++r;
|
||||
}
|
||||
}
|
||||
occupy(c, r, w, h);
|
||||
out.cells[i] = {c, r};
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
QRectF cellBox(const CellPos &cell, qreal colW, int spanW, int spanH) {
|
||||
const qreal x = cell.col * (colW + kColGap);
|
||||
const qreal y = cell.row * (kCellH + kRowGap);
|
||||
const qreal w = spanW * colW + std::max(0, spanW - 1) * kColGap;
|
||||
const qreal h = spanH * kCellH + std::max(0, spanH - 1) * kRowGap;
|
||||
return QRectF(x, y, w, h);
|
||||
}
|
||||
|
||||
} // namespace Layout
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <QRectF>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
namespace Layout {
|
||||
|
||||
inline constexpr qreal kCellH = 92.0;
|
||||
inline constexpr qreal kColGap = 6.0;
|
||||
inline constexpr qreal kRowGap = 12.0;
|
||||
|
||||
struct HomeSlot {
|
||||
QString component; // non-empty for app slots
|
||||
int widgetId = -1; // >= 0 for Android AppWidget slots
|
||||
QString provider; // widget provider component
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
int w = 1;
|
||||
int h = 1;
|
||||
|
||||
bool isWidget() const { return widgetId >= 0 || !provider.isEmpty(); }
|
||||
};
|
||||
|
||||
struct CellPos {
|
||||
int col = 0;
|
||||
int row = 0;
|
||||
};
|
||||
|
||||
struct PackedGrid {
|
||||
QVector<CellPos> cells; // Same length as slots; invalid (-1, -1) if hidden.
|
||||
int rows = 0;
|
||||
};
|
||||
|
||||
PackedGrid packHome(const QVector<HomeSlot> &items, int cols, const QSet<QString> &hidden);
|
||||
QRectF cellBox(const CellPos &cell, qreal colW, int spanW = 1, int spanH = 1);
|
||||
|
||||
} // namespace Layout
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "launcher_window.h"
|
||||
|
||||
#include <qt2wasm/gui.h>
|
||||
|
||||
int qt2wasm_gui_main(int, char **) {
|
||||
auto *w = new LauncherWindow;
|
||||
w->show();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user