Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/window_size/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.packages
pubspec.lock
1 change: 1 addition & 0 deletions plugins/window_size/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../../analysis_options.yaml
29 changes: 29 additions & 0 deletions plugins/window_size/common/channel_constants.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "plugins/window_size/common/channel_constants.h"

namespace plugins_window_size {

const char kChannelName[] = "flutter/windowsize";

const char kGetScreenListMethod[] = "getScreenList";
const char kGetWindowInfoMethod[] = "getWindowInfo";
const char kSetWindowFrameMethod[] = "setWindowFrame";

const char kFrameKey[] = "frame";
const char kVisibleFrameKey[] = "visibleFrame";
const char kScaleFactorKey[] = "scaleFactor";
const char kScreenKey[] = "screen";

} // namespace plugins_window_size
70 changes: 70 additions & 0 deletions plugins/window_size/common/channel_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLUGINS_WINDOW_SIZE_COMMON_CHANNEL_CONSTANTS_H_
#define PLUGINS_WINDOW_SIZE_COMMON_CHANNEL_CONSTANTS_H_

namespace plugins_window_size {

// This file contains constants used in the platform channel, which are shared
// across all native platform implementations.

// The name of the plugin's platform channel.
extern const char kChannelName[];

// The method name to request information about the available screens.
//
// Returns a list of screen info maps; see keys below.
extern const char kGetScreenListMethod[];

// The method name to request information about the window containing the
// Flutter instance.
//
// Returns a list of window info maps; see keys below.
extern const char kGetWindowInfoMethod[];

// The method name to set the frame of a window.
//
// Takes a frame array, as documented for the value of kFrameKey.
extern const char kSetWindowFrameMethod[];

// Keys for screen and window maps returned by kGetScreenListMethod.

// The frame of a screen or window. The value is an array of four doubles:
// [left, top, width, height]
extern const char kFrameKey[];

// The frame of a screen available for use by applications. The value format
// is the same as kFrameKey's.
//
// Only used for screens.
extern const char kVisibleFrameKey[];

// The scale factor for a screen or window, as a double.
//
// This is the number of pixels per screen coordinate, and thus the ratio
// between sizes as seen by Flutter and sizes in native screen coordinates.
extern const char kScaleFactorKey[];

// The screen containing this window, if any. The value is a screen map, or null
// if the window is not visible on a screen.
//
// Only used for windows.
//
// If a window is on multiple screens, it is up to the platform to decide which
// screen to report.
extern const char kScreenKey[];

} // namespace plugins_window_size

#endif // PLUGINS_WINDOW_SIZE_COMMON_CHANNEL_CONSTANTS_H_
35 changes: 35 additions & 0 deletions plugins/window_size/lib/src/platform_window.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:ui';

import 'screen.dart';

/// Represents a window, containing information about its size, position, and
/// properties.
///
/// TODO: Evaluate how to reconcile this with the highly mobile-centric Window
/// class in dart::ui
class PlatformWindow {
/// Create a new window.
PlatformWindow(this.frame, this.scaleFactor, this.screen);

/// The frame of the screen, in screen coordinates.
final Rect frame;

/// The number of pixels per screen coordinate for this screen.
final double scaleFactor;

/// The (or a) screen containing this window, if any.
final Screen screen;
}
31 changes: 31 additions & 0 deletions plugins/window_size/lib/src/screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:ui';

/// Represents a screen, containing information about its size, position, and
/// properties.
class Screen {
/// Create a new screen.
Screen(this.frame, this.visibleFrame, this.scaleFactor);

/// The frame of the screen, in screen coordinates.
final Rect frame;

/// The portion of the screen's frame that is available for use by application
/// windows. E.g., on macOS, this excludes the menu bar.
final Rect visibleFrame;

/// The number of pixels per screen coordinate for this screen.
final double scaleFactor;
}
113 changes: 113 additions & 0 deletions plugins/window_size/lib/src/window_size_channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async';
import 'dart:ui';

import 'package:flutter/services.dart';

import 'platform_window.dart';
import 'screen.dart';

// Plugin channel constants. See common/channel_constants.h for details.
const String _windowSizeChannelName = 'flutter/windowsize';
const String _getScreenListMethod = 'getScreenList';
const String _getCurrentScreenMethod = 'getCurrentScreen';
const String _getWindowInfoMethod = 'getWindowInfo';
const String _setWindowFrameMethod = 'setWindowFrame';
const String _frameKey = 'frame';
const String _visibleFrameKey = 'visibleFrame';
const String _scaleFactorKey = 'scaleFactor';
const String _screenKey = 'screen';

/// A singleton object that handles the interaction with the platform channel.
class WindowSizeChannel {
/// Private constructor.
WindowSizeChannel._();

final MethodChannel _platformChannel =
const MethodChannel(_windowSizeChannelName);

/// The static instance of the menu channel.
static final WindowSizeChannel instance = new WindowSizeChannel._();

/// Returns a list of screens.
Future<List<Screen>> getScreenList() async {
try {
final screenList = <Screen>[];
final response =
await _platformChannel.invokeMethod(_getScreenListMethod);

for (final screenInfo in response) {
screenList.add(_screenFromInfoMap(screenInfo));
}
return screenList;
} on PlatformException catch (e) {
print('Platform exception getting screen list: ${e.message}');
}
return <Screen>[];
}

/// Returns information about the window containing this Flutter instance.
Future<PlatformWindow> getWindowInfo() async {
try {
final response =
await _platformChannel.invokeMethod(_getWindowInfoMethod);

return PlatformWindow(
_rectFromLTWHList(response[_frameKey].cast<double>()),
response[_scaleFactorKey],
_screenFromInfoMap(response[_screenKey]));
} on PlatformException catch (e) {
print('Platform exception getting window info: ${e.message}');
}
return null;
}

/// Sets the frame of the window containing this Flutter instance, in
/// screen coordinates.
///
/// The platform may adjust the frame as necessary if the provided frame would
/// cause significant usability issues (e.g., a window with no visible portion
/// that can be used to move the window).
void setWindowFrame(Rect frame) async {
try {
await _platformChannel.invokeMethod(_setWindowFrameMethod,
[frame.left, frame.top, frame.width, frame.height]);
} on PlatformException catch (e) {
print('Platform exception setting window frame: ${e.message}');
}
}

/// Given an array of the form [left, top, width, height], return the
/// corresponding [Rect].
///
/// Used for frame deserialization in the platform channel.
Rect _rectFromLTWHList(List<double> ltwh) {
return Rect.fromLTWH(ltwh[0], ltwh[1], ltwh[2], ltwh[3]);
}

/// Given a map of information about a screen, return the corresponding
/// [Screen] object, or null.
///
/// Used for screen deserialization in the platform channel.
Screen _screenFromInfoMap(Map<dynamic, dynamic> map) {
if (map == null) {
return null;
}
return Screen(
_rectFromLTWHList(map[_frameKey].cast<double>()),
_rectFromLTWHList(map[_visibleFrameKey].cast<double>()),
map[_scaleFactorKey]);
}
}
51 changes: 51 additions & 0 deletions plugins/window_size/lib/src/window_size_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async';
import 'dart:ui';

import 'platform_window.dart';
import 'screen.dart';
import 'window_size_channel.dart';

/// Returns a list of [Screen]s for the current screen configuration.
///
/// It is possible for this list to be empty, if the machine is running in
/// a headless mode.
Future<List<Screen>> getScreenList() async {
return await WindowSizeChannel.instance.getScreenList();
}

/// Returns the [Screen] showing the window that contains this Flutter instance.
///
/// If the window is not being displayed, returns null. If the window is being
/// displayed on multiple screens, the platform can return any of those screens.
Future<Screen> getCurrentScreen() async {
final windowInfo = await WindowSizeChannel.instance.getWindowInfo();
return windowInfo.screen;
}

/// Returns information about the window containing this Flutter instance.
Future<PlatformWindow> getWindowInfo() async {
return await WindowSizeChannel.instance.getWindowInfo();
}

/// Sets the frame of the window containing this Flutter instance, in
/// screen coordinates.
///
/// The platform may adjust the frame as necessary if the provided frame would
/// cause significant usability issues (e.g., a window with no visible portion
/// that can be used to move the window).
void setWindowFrame(Rect frame) async {
WindowSizeChannel.instance.setWindowFrame(frame);
}
16 changes: 16 additions & 0 deletions plugins/window_size/lib/window_size.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export 'src/platform_window.dart';
export 'src/screen.dart';
export 'src/window_size_utils.dart';
25 changes: 25 additions & 0 deletions plugins/window_size/macos/FLEWindowSizePlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

#import <FlutterMacOS/FlutterMacOS.h>

/**
* A FlutterPlugin to manage macOS's shared NSColorPanel singleton.
* Responsible for managing the panel's display state and sending selected color data to Flutter.
*/
@interface FLEWindowSizePlugin : NSObject <FLEPlugin, NSWindowDelegate>

@end
Loading