Codebase list gnome-maps / 78fb17c
sidebar: Fix drag-and-dropping route entries This was not working since the GTK4 port. Marcus Lundblad 1 year, 7 months ago
1 changed file(s) with 21 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
2828 import {Application} from './application.js';
2929 import {InstructionRow} from './instructionRow.js';
3030 import {PlaceStore} from './placeStore.js';
31 import {QueryPoint} from './routeQuery.js';
3132 import {RouteEntry} from './routeEntry.js';
3233 import {RouteQuery} from './routeQuery.js';
3334 import {StoredRoute} from './storedRoute.js';
513514 // Iterate over points and establish the new order of places
514515 _reorderRoutePoints(srcIndex, destIndex) {
515516 let points = this._query.points;
516 let srcPlace = this._draggedPoint.place;
517 let srcPlace = this._query.points[srcIndex].place;
517518
518519 // Determine if we are swapping from "above" or "below"
519520 let step = (srcIndex < destIndex) ? -1 : 1;
549550 this._query.thaw_notify();
550551 }
551552
552 _onDragDrop(row) {
553 let srcIndex = this._query.points.indexOf(this._draggedPoint);
553 _onDragDrop(row, point) {
554 let srcIndex = this._query.points.indexOf(point);
554555 let destIndex = row.get_index();
555556
556557 this._reorderRoutePoints(srcIndex, destIndex);
557558
558 return true;
559 }
560
561 _dragHighlightRow(row) {
562 row.opacity = 0.6;
563 }
564
565 _dragUnhighlightRow(row) {
566 row.opacity = 1.0;
567 }
568
569 // Set the opacity of the row we are currently dragging above
570 // to semi transparent.
571 _onDragMotion(row, context, x, y, time) {
572 let routeEntry = row.get_child();
573
574 if (this._draggedPoint && this._draggedPoint !== routeEntry.point) {
575 this._dragHighlightRow(row);
576 Gdk.drag_status(context, Gdk.DragAction.MOVE, time);
577 } else
578 Gdk.drag_status(context, 0, time);
579559 return true;
580560 }
581561
582562 // Drag ends, show the dragged row again.
583563 _onDragEnd(row) {
584 this._draggedPoint = null;
585
586 // Restore to natural height
587 row.height_request = -1;
588 row.get_child().show();
589 }
590
591 // Drag begins, set the correct drag icon and hide the dragged row.
564 row.opacity = 1.0;
565 }
566
567 _onDragPrepare(point, source, x, y) {
568 return Gdk.ContentProvider.new_for_value(point);
569 }
570
571 // Drag begins, set the correct drag icon and dim the dragged row.
592572 _onDragBegin(source, row) {
593573 let routeEntry = row.get_child();
594574 let {x, y, width, height} = row.get_allocation();
595
596 this._draggedPoint = routeEntry.point;
597
598 // Set a fixed height on the row to prevent the sidebar height
599 // to shrink while dragging a row.
600 row.height_request = height;
601 row.get_child().hide();
602
603 let paintable = new Gtk.WidgetPaintable(row);
575 let paintable = new Gtk.WidgetPaintable({ widget: routeEntry });
604576
605577 source.set_icon(paintable, 0, 0);
578 row.opacity = 0.6;
606579 }
607580
608581 // Set up drag and drop between RouteEntrys. The drag source is from a
615588
616589 dragIcon.add_controller(dragSource);
617590
591 dragSource.connect('prepare',
592 this._onDragPrepare.bind(this, routeEntry.point));
618593 dragSource.connect('drag-begin',
619 (source, drag, widget) => this._onDragBegin(source, row));
594 (source, drag, widget) =>
595 this._onDragBegin(source, row));
620596 dragSource.connect('drag-end',
621597 (source, dele, data) => this._onDragEnd(row));
622598
623 let dropTarget = Gtk.DropTarget.new(RouteEntry, Gdk.DragAction.MOVE);
599 let dropTarget = Gtk.DropTarget.new(QueryPoint, Gdk.DragAction.COPY);
624600
625601 row.add_controller(dropTarget);
626602
627603 dropTarget.connect('drop',
628 (target, value, x, y, data) => this._onDragDrop(target));
604 (target, value, x, y, data) =>
605 this._onDragDrop(row, value));
629606 }
630607 }
631608