Support long-press drag-to-move for home icons and widgets

This commit is contained in:
nandi committed 2026-11-13 11:15:34 -08:00
1 parent f5c3b8ac60
commit 52d03decbd
2 files changed
+192 -13

No files matched your search

@@ -32,7 +32,9 @@ import android.util.SizeF;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
@@ -106,7 +108,13 @@ public class LauncherActivity extends NativeActivity {
launcher = getSystemService(LauncherApps.class);
launcher.registerCallback(packages, main);
widgets = AppWidgetManager.getInstance(this);
widgetHost = new AppWidgetHost(this, WIDGET_HOST_ID);
widgetHost = new AppWidgetHost(this, WIDGET_HOST_ID) {
@Override
protected AppWidgetHostView onCreateView(android.content.Context context, int appWidgetId,
AppWidgetProviderInfo appWidget) {
return new DraggableWidgetView(appWidgetId);
}
};
widgetLayer = new FrameLayout(this);
seeThrough();
getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
@@ -475,6 +483,101 @@ public class LauncherActivity extends NativeActivity {
});
}
private final class DraggableWidgetView extends AppWidgetHostView {
private final int widgetId;
private final int touchSlop;
private boolean lifted = false;
private float downRawX, downRawY, lastRawX, lastRawY;
private int startWinX, startWinY;
private final Runnable longPress;
DraggableWidgetView(int widgetId) {
super(LauncherActivity.this);
this.widgetId = widgetId;
this.touchSlop = ViewConfiguration.get(LauncherActivity.this).getScaledTouchSlop();
this.longPress = () -> {
lifted = true;
requestDisallowInterceptTouchEvent(true);
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
setAlpha(0.88f);
setScaleX(1.04f);
setScaleY(1.04f);
WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
startWinX = lp.x;
startWinY = lp.y;
nativeOnEvent("widget-lift:" + this.widgetId + ":" + lastRawX + ":" + lastRawY);
};
}
private void handleMove(MotionEvent ev) {
lastRawX = ev.getRawX();
lastRawY = ev.getRawY();
if (lifted) {
WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
lp.x = Math.round(startWinX + (lastRawX - downRawX));
lp.y = Math.round(startWinY + (lastRawY - downRawY));
getWindowManager().updateViewLayout(this, lp);
nativeOnEvent("widget-drag:" + lastRawX + ":" + lastRawY);
} else if (Math.hypot(lastRawX - downRawX, lastRawY - downRawY) > touchSlop) {
main.removeCallbacks(longPress);
}
}
private void handleEnd(MotionEvent ev) {
main.removeCallbacks(longPress);
if (lifted) {
lifted = false;
setAlpha(1.0f);
setScaleX(1.0f);
setScaleY(1.0f);
nativeOnEvent("widget-drop:" + widgetId + ":" + ev.getRawX() + ":" + ev.getRawY()
+ ":" + downRawX + ":" + downRawY);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
lifted = false;
downRawX = lastRawX = ev.getRawX();
downRawY = lastRawY = ev.getRawY();
main.removeCallbacks(longPress);
main.postDelayed(longPress, 450);
break;
case MotionEvent.ACTION_MOVE:
handleMove(ev);
if (lifted) return true;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
boolean wasLifted = lifted;
handleEnd(ev);
if (wasLifted) return true;
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
handleMove(ev);
if (lifted) return true;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
boolean wasLifted = lifted;
handleEnd(ev);
if (wasLifted) return true;
break;
}
return super.onTouchEvent(ev);
}
}
public void placeWidgets(String json) {
main.post(() -> {
lastPlacedWidgetsJson = json;
+88 -12
View File
@@ -311,6 +311,55 @@ void LauncherWindow::pollHostEvents() {
saveHome();
update();
}
} else if (ev.startsWith(QLatin1String("widget-lift:"))) {
const QStringList parts = ev.mid(12).split(QLatin1Char(':'));
if (parts.size() >= 3) {
const int id = parts[0].toInt();
const qreal dpr = devicePixelRatio();
for (int i = 0; i < m_home.size(); ++i) {
if (m_home[i].widgetId == id) {
m_liftedHome = i;
m_liftPos = QPointF(parts[1].toDouble() / dpr, parts[2].toDouble() / dpr);
update();
break;
}
}
}
} else if (ev.startsWith(QLatin1String("widget-drag:"))) {
const QStringList parts = ev.mid(12).split(QLatin1Char(':'));
if (parts.size() >= 2 && m_liftedHome >= 0) {
const qreal dpr = devicePixelRatio();
m_liftPos = QPointF(parts[0].toDouble() / dpr, parts[1].toDouble() / dpr);
update();
}
} else if (ev.startsWith(QLatin1String("widget-drop:"))) {
const QStringList parts = ev.mid(12).split(QLatin1Char(':'));
if (parts.size() >= 5) {
const int id = parts[0].toInt();
const qreal dpr = devicePixelRatio();
const QPointF dropPos(parts[1].toDouble() / dpr, parts[2].toDouble() / dpr);
const QPointF downPos(parts[3].toDouble() / dpr, parts[4].toDouble() / dpr);
const qreal dist = std::hypot(dropPos.x() - downPos.x(), dropPos.y() - downPos.y());
int idx = -1;
for (int i = 0; i < m_home.size(); ++i) {
if (m_home[i].widgetId == id) { idx = i; break; }
}
m_liftedHome = -1;
if (idx >= 0) {
if (dist > 12.0) {
const qreal localX = dropPos.x() - gridX();
const qreal localY = dropPos.y() - (gridTop() - m_homeScroll);
const int maxCol = std::max(0, homeCols() - m_home[idx].w);
const int col = std::clamp(int(localX / (homeColW() + Layout::kColGap)), 0, maxCol);
const int row = std::max(0, int(localY / (Layout::kCellH + Layout::kRowGap)));
m_home[idx].x = col;
m_home[idx].y = row;
saveHome();
}
m_lastPlacedWidgets.clear();
update();
}
}
}
}
gui_list_string_free(&evs);
@@ -616,11 +665,26 @@ void LauncherWindow::drawHome(QPainter &p) {
}
if (m_liftedHome >= 0 && m_liftedHome < m_home.size()) {
const QString comp = m_home[m_liftedHome].component;
const int appIdx = m_appIndex.value(comp, -1);
const QString label = appIdx >= 0 ? m_apps[appIdx].label : comp;
const QRectF r(m_liftPos.x() - cw / 2.0, m_liftPos.y() - Layout::kCellH / 2.0, cw, Layout::kCellH);
drawIconAndLabel(p, r, comp, label, 52, true);
const auto &lifted = m_home[m_liftedHome];
const qreal localX = m_liftPos.x() - gridX();
const qreal localY = m_liftPos.y() - (gridTop() - m_homeScroll);
if (localX >= 0 && localY >= 0) {
const int maxCol = std::max(0, homeCols() - lifted.w);
const int col = std::clamp(int(localX / (homeColW() + Layout::kColGap)), 0, maxCol);
const int row = std::max(0, int(localY / (Layout::kCellH + Layout::kRowGap)));
QRectF target = Layout::cellBox({col, row}, cw, lifted.w, lifted.h);
target.translate(gridX(), gridTop() - m_homeScroll);
p.setPen(QPen(QColor(255, 255, 255, 110), 1.5, Qt::DashLine));
p.setBrush(QColor(255, 255, 255, 24));
p.drawRoundedRect(target, 16.0, 16.0);
}
if (!lifted.isWidget()) {
const QString comp = lifted.component;
const int appIdx = m_appIndex.value(comp, -1);
const QString label = appIdx >= 0 ? m_apps[appIdx].label : comp;
const QRectF r(m_liftPos.x() - cw / 2.0, m_liftPos.y() - Layout::kCellH / 2.0, cw, Layout::kCellH);
drawIconAndLabel(p, r, comp, label, 52, true);
}
}
}
@@ -783,23 +847,32 @@ void LauncherWindow::mouseReleaseEvent(QMouseEvent *event) {
const QPointF pos = event->position();
if (m_liftedHome >= 0) {
const int lifted = m_liftedHome;
m_liftedHome = -1;
if (!m_dragging) {
update();
openSheet(SheetKind::HomeItem, lifted);
return;
}
const int target = hitHomeSlot(pos);
if (target >= 0 && target != m_liftedHome && target < m_home.size()) {
std::swap(m_home[m_liftedHome], m_home[target]);
if (target >= 0 && target != lifted && target < m_home.size() && !m_home[target].isWidget()) {
std::swap(m_home[lifted].x, m_home[target].x);
std::swap(m_home[lifted].y, m_home[target].y);
std::swap(m_home[lifted], m_home[target]);
saveHome();
} else if (target < 0) {
// Drop into grid coordinates if over empty cell
const qreal localX = pos.x() - gridX();
const qreal localY = pos.y() - (gridTop() - m_homeScroll);
if (localX >= 0 && localY >= 0) {
const int col = std::clamp(int(localX / (homeColW() + Layout::kColGap)), 0, homeCols() - 1);
const int maxCol = std::max(0, homeCols() - m_home[lifted].w);
const int col = std::clamp(int(localX / (homeColW() + Layout::kColGap)), 0, maxCol);
const int row = std::max(0, int(localY / (Layout::kCellH + Layout::kRowGap)));
m_home[m_liftedHome].x = col;
m_home[m_liftedHome].y = row;
m_home[lifted].x = col;
m_home[lifted].y = row;
saveHome();
}
}
m_liftedHome = -1;
update();
return;
}
@@ -915,7 +988,10 @@ void LauncherWindow::timerEvent(QTimerEvent *event) {
if (m_drawerOpen) {
if (m_pressedDrawer >= 0) openSheet(SheetKind::DrawerApp, m_pressedDrawer);
} else if (m_pressedHome >= 0) {
openSheet(SheetKind::HomeItem, m_pressedHome);
m_liftedHome = m_pressedHome;
m_liftPos = m_pressPos;
ultra_launcher_android_haptic();
update();
} else {
openSheet(SheetKind::Menu, -1);
}