Skip to content

Commit

Permalink
flutter: Added floating item and DockRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Dec 8, 2024
1 parent a7413e5 commit a4cbba6
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/flutter-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v4
with:
submodules: true

- name: Install dependencies on Ubuntu
run: |
Expand Down
1 change: 0 additions & 1 deletion examples/flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

import 'dart:developer';
import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:KDDockWidgets/KDDockWidgets.dart';

import 'package:KDDockWidgets/widgets/DockWidget.dart';
Expand Down
4 changes: 3 additions & 1 deletion src/flutter/dart/lib/KDDockWidgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

library kddockwidgets;

import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:KDDockWidgets/models/GeometryItem.dart';
import 'package:KDDockWidgets/models/Separator.dart';
import 'package:KDDockWidgets/private/Bindings.dart';
Expand All @@ -24,6 +23,9 @@ import 'package:signals_slots/signals_slots.dart';
part 'models/Group.dart';
part 'models/DropArea.dart';
part 'models/TitleBar.dart';
part 'models/FloatingItem.dart';
part 'models/DockRegistry.dart';
part 'models/DockItem.dart';

class DropLocation {
static const int DropLocation_None = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/flutter/dart/lib/models/DockItem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
Contact KDAB at <[email protected]> for commercial licensing options.
*/

import 'package:flutter/widgets.dart';
import 'package:signals_slots/signals_slots.dart';
part of kddockwidgets;

/// represents the state of a dock widget
Expand All @@ -24,7 +23,9 @@ class DockItem {
DockItem({
required this.uniqueName,
this.guestWidget,
});
}) {
DockRegistry.instance.addDockItem(this);
}

String get title {
if (_title.isNotEmpty) return _title;
Expand Down
76 changes: 76 additions & 0 deletions src/flutter/dart/lib/models/DockRegistry.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
Author: Sérgio Martins <[email protected]>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <[email protected]> for commercial licensing options.
*/

part of kddockwidgets;

class DockRegistry {
static final DockRegistry _instance = DockRegistry._internal();

final floatingItems = <FloatingItem>[];
final dockItems = <DockItem>[];

void addFloatingItem(FloatingItem floatingItem) {
if (floatingItems.contains(floatingItem)) {
throw Exception("FloatingItem already exists in registry");
}

if (floatingItemForDropArea(floatingItem.dropArea) != null) {
throw Exception("Already have a floating item for this drop area");
}

floatingItems.add(floatingItem);
}

void removeFloatingItem(FloatingItem floatingItem) {
if (!floatingItems.contains(floatingItem)) {
throw Exception("FloatingItem doesn't exists in registry");
}

floatingItems.remove(floatingItem);
}

void addDockItem(DockItem dockItem) {
if (dockItems.contains(dockItem)) {
throw Exception("DockItem already exists in registry");
}

dockItems.add(dockItem);
}

void removeDockItem(DockItem dockItem) {
if (!dockItems.contains(dockItem)) {
throw Exception("DockItem doesn't exists in registry");
}

dockItems.remove(dockItem);
}

FloatingItem? floatingItemForDropArea(DropArea da) {
for (FloatingItem floatingItem in floatingItems) {
if (floatingItem.dropArea == da) {
return floatingItem;
}
}

return null;
}

bool containsFloatingItem(FloatingItem floatingItem) {
return floatingItems.contains(floatingItem);
}

factory DockRegistry() {
return _instance;
}
DockRegistry._internal();

static DockRegistry get instance => _instance;
}
4 changes: 4 additions & 0 deletions src/flutter/dart/lib/models/DropArea.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class DropArea implements ffi.Finalizable {
return _groups.any((group) => group.containsDockItem(name));
}

bool hasGroups() {
return _groups.isNotEmpty;
}

void setLayoutSize(int width, int height) {
Bindings.instance.nativeLibrary
.on_flutter_droparea_widget_resized(_hostCpp.cast(), width, height);
Expand Down
43 changes: 43 additions & 0 deletions src/flutter/dart/lib/models/FloatingItem.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
Author: Sérgio Martins <[email protected]>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <[email protected]> for commercial licensing options.
*/

part of kddockwidgets;

class FloatingItem implements ItemWithTitleBar {
final dropArea = DropArea();
late final TitleBar titleBar;
late final Connection _groupCountChangedConnection;

FloatingItem() {
DockRegistry.instance.addFloatingItem(this);
titleBar = TitleBar(this);

_groupCountChangedConnection =
dropArea.layoutChanged.connect(onGroupCountChanged);
}

void onGroupCountChanged() {
if (!dropArea.hasGroups()) {
close();
}
}

@override
void close() {
if (!DockRegistry.instance.containsFloatingItem(this)) {
// we're closed already, can be removed once we can _groupCountChangedConnection.disconnect()
return;
}

// _groupCountChangedConnection.disconnect(); signals_slot bug
DockRegistry.instance.removeFloatingItem(this);
}
}
3 changes: 2 additions & 1 deletion src/flutter/dart/lib/models/Group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void _geometryChangedCallback(ffi.Pointer<ffi.Void> guest, int x, int y,
}
}

class Group extends GeometryItem implements ffi.Finalizable {
class Group extends GeometryItem implements ffi.Finalizable, ItemWithTitleBar {
static final _finalizer =
ffi.NativeFinalizer(finalizerFunc("delete_guest").cast());

Expand Down Expand Up @@ -160,6 +160,7 @@ class Group extends GeometryItem implements ffi.Finalizable {
}
}

@override
void close() {
dropArea._removeGroup(this);
}
Expand Down
15 changes: 10 additions & 5 deletions src/flutter/dart/lib/models/TitleBar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@

part of kddockwidgets;

// Floating windows and tab groups have titlebars
abstract class ItemWithTitleBar {
// called when close button on the titlebar is clicked
void close();
}

class TitleBar {
String title = "";
bool isExplicitlyHidden = false;
final Group? _group;
final ItemWithTitleBar _itemWithTitleBar;

TitleBar(Group? group, {this.title = ""}) : _group = group {}
TitleBar(ItemWithTitleBar itemWithTitleBar, {this.title = ""})
: _itemWithTitleBar = itemWithTitleBar {}

void onCloseClicked() {
if (_group != null) {
_group.close();
}
_itemWithTitleBar.close();
}

void onFloatClicked() {}
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/DockWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Contact KDAB at <[email protected]> for commercial licensing options.
*/

import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:flutter/widgets.dart';
import 'package:KDDockWidgets/KDDockWidgets.dart';

class DockWidget extends StatefulWidget {
final DockItem dockItem;
Expand Down
1 change: 0 additions & 1 deletion src/flutter/dart/lib/widgets/GroupWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

import 'package:KDDockWidgets/KDDockWidgets.dart';
import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:KDDockWidgets/widgets/PositionedWidget.dart';
import 'package:KDDockWidgets/widgets/TabBarWidget.dart';
import 'package:KDDockWidgets/widgets/TitleBarWidget.dart';
Expand Down
1 change: 0 additions & 1 deletion src/flutter/dart/test/models/droparea_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Contact KDAB at <[email protected]> for commercial licensing options.
*/

import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:KDDockWidgets/KDDockWidgets.dart';
import 'package:KDDockWidgets/private/Bindings.dart';
Expand Down
1 change: 0 additions & 1 deletion tests/flutter/integration_test/ui_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import 'dart:io';
import 'dart:ui';

import 'package:KDDockWidgets/models/DockItem.dart';
import 'package:KDDockWidgets/KDDockWidgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Expand Down
2 changes: 1 addition & 1 deletion tests/reference-images

0 comments on commit a4cbba6

Please sign in to comment.