feat: initial Cutdex application
Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
This commit is contained in:
commit
27e77fff39
718 files changed
+183958
No files matched your search
@@ -0,0 +1,17 @@
|
||||
flutter/
|
||||
|
||||
# Visual Studio user-specific files.
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# Visual Studio build-related files.
|
||||
x64/
|
||||
x86/
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!*.[Cc]ache/
|
||||
@@ -0,0 +1,64 @@
|
||||
# The Flutter tooling requires that developers have a version of Visual Studio
|
||||
# installed that includes CMake 3.14 or later. You should not increase this
|
||||
# version, as doing so will cause the plugin to fail to compile for some
|
||||
# customers of the plugin.
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
# Project-level configuration.
|
||||
set(PROJECT_NAME "cutdex_native")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
||||
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
|
||||
# versions of CMake.
|
||||
cmake_policy(VERSION 3.14...3.25)
|
||||
|
||||
# This value is used when generating builds using this plugin, so it must
|
||||
# not be changed
|
||||
set(PLUGIN_NAME "cutdex_native_plugin")
|
||||
|
||||
# Any new source files that you add to the plugin should be added here.
|
||||
list(APPEND PLUGIN_SOURCES
|
||||
"cutdex_native_plugin.cpp"
|
||||
"cutdex_native_plugin.h"
|
||||
"window_chrome.cpp"
|
||||
"native_file_drop.cpp"
|
||||
)
|
||||
|
||||
# Define the plugin library target. Its name must not be changed (see comment
|
||||
# on PLUGIN_NAME above).
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"include/cutdex_native/cutdex_native_plugin_c_api.h"
|
||||
"cutdex_native_plugin_c_api.cpp"
|
||||
${PLUGIN_SOURCES}
|
||||
)
|
||||
|
||||
# Apply a standard set of build settings that are configured in the
|
||||
# application-level CMakeLists.txt. This can be removed for plugins that want
|
||||
# full control over build settings.
|
||||
apply_standard_settings(${PLUGIN_NAME})
|
||||
|
||||
# Symbols are hidden by default to reduce the chance of accidental conflicts
|
||||
# between plugins. This should not be removed; any symbols that should be
|
||||
# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro.
|
||||
set_target_properties(${PLUGIN_NAME} PROPERTIES
|
||||
CXX_VISIBILITY_PRESET hidden)
|
||||
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
|
||||
|
||||
# Source include directories and library dependencies. Add any plugin-specific
|
||||
# dependencies here.
|
||||
target_include_directories(${PLUGIN_NAME} INTERFACE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin dwmapi comctl32 ole32 shell32 uuid)
|
||||
|
||||
# List of absolute paths to libraries that should be bundled with the plugin.
|
||||
# This list could contain prebuilt libraries, or libraries created by an
|
||||
# external build triggered from this build file.
|
||||
set(cutdex_native_bundled_libraries
|
||||
""
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
target_compile_definitions(${PLUGIN_NAME} PRIVATE NOMINMAX)
|
||||
target_compile_definitions(${PLUGIN_NAME} PRIVATE
|
||||
"CUTDEX_APP_VERSION=\"${FLUTTER_VERSION}\""
|
||||
"CUTDEX_APP_BUILD=\"${FLUTTER_VERSION_BUILD}\"")
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "cutdex_native_plugin.h"
|
||||
#include <flutter/standard_method_codec.h>
|
||||
|
||||
namespace cutdex_native {
|
||||
void CutdexNativePlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows* registrar) {
|
||||
registrar->AddPlugin(std::make_unique<CutdexNativePlugin>(registrar));
|
||||
}
|
||||
|
||||
CutdexNativePlugin::CutdexNativePlugin(flutter::PluginRegistrarWindows* registrar)
|
||||
: registrar_(registrar) {
|
||||
device_info_ = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
||||
registrar->messenger(), "cutdex_native/device_info",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
device_info_->SetMethodCallHandler([](const auto& call, auto result) {
|
||||
if (call.method_name() != "getApplicationInfo") {
|
||||
result->NotImplemented();
|
||||
return;
|
||||
}
|
||||
result->Success(flutter::EncodableValue(flutter::EncodableMap{
|
||||
{flutter::EncodableValue("appVersion"), flutter::EncodableValue(CUTDEX_APP_VERSION)},
|
||||
{flutter::EncodableValue("appBuild"), flutter::EncodableValue(CUTDEX_APP_BUILD)}}));
|
||||
});
|
||||
const HWND view = registrar->GetView()->GetNativeWindow();
|
||||
chrome_ = std::make_unique<WindowChrome>(
|
||||
GetAncestor(view, GA_ROOT), view, registrar->messenger());
|
||||
drop_ = std::make_unique<NativeFileDrop>(view, registrar->messenger());
|
||||
delegate_id_ = registrar->RegisterTopLevelWindowProcDelegate(
|
||||
[this](HWND, UINT message, WPARAM wparam, LPARAM lparam)
|
||||
-> std::optional<LRESULT> {
|
||||
return chrome_->HandleMessage(message, wparam, lparam);
|
||||
});
|
||||
}
|
||||
|
||||
CutdexNativePlugin::~CutdexNativePlugin() {
|
||||
registrar_->UnregisterTopLevelWindowProcDelegate(delegate_id_);
|
||||
drop_.reset();
|
||||
chrome_.reset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef FLUTTER_PLUGIN_CUTDEX_NATIVE_PLUGIN_H_
|
||||
#define FLUTTER_PLUGIN_CUTDEX_NATIVE_PLUGIN_H_
|
||||
#include <flutter/plugin_registrar_windows.h>
|
||||
#include <flutter/method_channel.h>
|
||||
#include "window_chrome.h"
|
||||
#include "native_file_drop.h"
|
||||
#include <memory>
|
||||
|
||||
namespace cutdex_native {
|
||||
class CutdexNativePlugin : public flutter::Plugin {
|
||||
public:
|
||||
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar);
|
||||
explicit CutdexNativePlugin(flutter::PluginRegistrarWindows* registrar);
|
||||
~CutdexNativePlugin() override;
|
||||
CutdexNativePlugin(const CutdexNativePlugin&) = delete;
|
||||
CutdexNativePlugin& operator=(const CutdexNativePlugin&) = delete;
|
||||
private:
|
||||
flutter::PluginRegistrarWindows* registrar_;
|
||||
int delegate_id_;
|
||||
std::unique_ptr<WindowChrome> chrome_;
|
||||
std::unique_ptr<NativeFileDrop> drop_;
|
||||
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> device_info_;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "include/cutdex_native/cutdex_native_plugin_c_api.h"
|
||||
|
||||
#include <flutter/plugin_registrar_windows.h>
|
||||
|
||||
#include "cutdex_native_plugin.h"
|
||||
|
||||
void CutdexNativePluginCApiRegisterWithRegistrar(
|
||||
FlutterDesktopPluginRegistrarRef registrar) {
|
||||
cutdex_native::CutdexNativePlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarManager::GetInstance()
|
||||
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef FLUTTER_PLUGIN_CUTDEX_NATIVE_PLUGIN_C_API_H_
|
||||
#define FLUTTER_PLUGIN_CUTDEX_NATIVE_PLUGIN_C_API_H_
|
||||
|
||||
#include <flutter_plugin_registrar.h>
|
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL
|
||||
#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void CutdexNativePluginCApiRegisterWithRegistrar(
|
||||
FlutterDesktopPluginRegistrarRef registrar);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // FLUTTER_PLUGIN_CUTDEX_NATIVE_PLUGIN_C_API_H_
|
||||
@@ -0,0 +1,367 @@
|
||||
#include "native_file_drop.h"
|
||||
|
||||
#include <flutter/encodable_value.h>
|
||||
#include <flutter/method_channel.h>
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <flutter_windows.h>
|
||||
#include <ole2.h>
|
||||
#include <shellapi.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using Value = flutter::EncodableValue;
|
||||
using Map = flutter::EncodableMap;
|
||||
using List = flutter::EncodableList;
|
||||
using Channel = flutter::MethodChannel<Value>;
|
||||
|
||||
struct Region {
|
||||
double x;
|
||||
double y;
|
||||
double width;
|
||||
double height;
|
||||
std::optional<std::vector<std::string>> allowed_extensions;
|
||||
std::optional<int64_t> maximum_file_count;
|
||||
|
||||
bool Contains(double px, double py) const {
|
||||
return px >= x && py >= y && px < x + width && py < y + height;
|
||||
}
|
||||
};
|
||||
|
||||
std::optional<double> Number(const Map& map, const char* key) {
|
||||
const auto entry = map.find(Value(key));
|
||||
if (entry == map.end()) return std::nullopt;
|
||||
const auto& value = entry->second;
|
||||
if (const auto* n = std::get_if<double>(&value)) return *n;
|
||||
if (const auto* n = std::get_if<int32_t>(&value)) return *n;
|
||||
if (const auto* n = std::get_if<int64_t>(&value)) {
|
||||
return static_cast<double>(*n);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<Region> ReadRegion(const Value* value) {
|
||||
const auto* map = value ? std::get_if<Map>(value) : nullptr;
|
||||
if (!map) return std::nullopt;
|
||||
const auto x = Number(*map, "x");
|
||||
const auto y = Number(*map, "y");
|
||||
const auto w = Number(*map, "width");
|
||||
const auto h = Number(*map, "height");
|
||||
if (!x || !y || !w || !h || !std::isfinite(*x) || !std::isfinite(*y) ||
|
||||
!std::isfinite(*w) || !std::isfinite(*h) || *w <= 0 || *h <= 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto extensions = map->find(Value("allowedExtensions"));
|
||||
const auto count = map->find(Value("maximumFileCount"));
|
||||
if (extensions == map->end() || count == map->end()) return std::nullopt;
|
||||
Region region{*x, *y, *w, *h, std::nullopt, std::nullopt};
|
||||
if (!std::holds_alternative<std::monostate>(extensions->second)) {
|
||||
const auto* values = std::get_if<List>(&extensions->second);
|
||||
if (!values) return std::nullopt;
|
||||
region.allowed_extensions.emplace();
|
||||
for (const auto& entry : *values) {
|
||||
const auto* extension = std::get_if<std::string>(&entry);
|
||||
if (!extension || extension->empty()) return std::nullopt;
|
||||
region.allowed_extensions->push_back(*extension);
|
||||
}
|
||||
}
|
||||
if (!std::holds_alternative<std::monostate>(count->second)) {
|
||||
if (const auto* count32 = std::get_if<int32_t>(&count->second)) {
|
||||
region.maximum_file_count = *count32;
|
||||
} else if (const auto* count64 = std::get_if<int64_t>(&count->second)) {
|
||||
region.maximum_file_count = *count64;
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (*region.maximum_file_count <= 0) return std::nullopt;
|
||||
}
|
||||
return region;
|
||||
}
|
||||
|
||||
std::string Utf8(const std::wstring& text) {
|
||||
const int count = static_cast<int>(text.size());
|
||||
const int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text.data(),
|
||||
count, nullptr, 0, nullptr, nullptr);
|
||||
if (size <= 0) return {};
|
||||
std::string result(static_cast<size_t>(size), '\0');
|
||||
if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text.data(), count,
|
||||
result.data(), size, nullptr, nullptr) != size) {
|
||||
return {};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
List ReadFiles(IDataObject* data) {
|
||||
List files;
|
||||
if (!data) return files;
|
||||
// Only existing file references are accepted. Virtual file descriptors and
|
||||
// promised contents are not existing local file references.
|
||||
FORMATETC format{CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
|
||||
STGMEDIUM medium{};
|
||||
if (FAILED(data->GetData(&format, &medium))) return files;
|
||||
if (medium.tymed == TYMED_HGLOBAL && medium.hGlobal) {
|
||||
const auto drop = static_cast<HDROP>(medium.hGlobal);
|
||||
const UINT count = DragQueryFileW(drop, 0xFFFFFFFF, nullptr, 0);
|
||||
for (UINT i = 0; i < count; ++i) {
|
||||
const UINT size = DragQueryFileW(drop, i, nullptr, 0);
|
||||
std::vector<wchar_t> buffer(static_cast<size_t>(size) + 1);
|
||||
if (size == 0 || DragQueryFileW(drop, i, buffer.data(), size + 1) != size) {
|
||||
files.clear();
|
||||
break;
|
||||
}
|
||||
const DWORD attributes = GetFileAttributesW(buffer.data());
|
||||
const auto path = Utf8(std::wstring(buffer.data(), size));
|
||||
if (path.empty() || attributes == INVALID_FILE_ATTRIBUTES ||
|
||||
(attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
|
||||
files.clear();
|
||||
break;
|
||||
}
|
||||
files.emplace_back(Map{{Value("path"), Value(path)}});
|
||||
}
|
||||
}
|
||||
ReleaseStgMedium(&medium);
|
||||
return files;
|
||||
}
|
||||
|
||||
bool Accepts(const Region& region, const List& files) {
|
||||
if (files.empty() ||
|
||||
(region.maximum_file_count &&
|
||||
files.size() > static_cast<uint64_t>(*region.maximum_file_count))) {
|
||||
return false;
|
||||
}
|
||||
if (!region.allowed_extensions) return true;
|
||||
for (const auto& entry : files) {
|
||||
const auto& file = std::get<Map>(entry);
|
||||
const auto& path = std::get<std::string>(file.at(Value("path")));
|
||||
const auto separator = path.find_last_of("/\\");
|
||||
const auto dot = path.find_last_of('.');
|
||||
if (dot == std::string::npos ||
|
||||
(separator != std::string::npos && dot <= separator)) return false;
|
||||
auto extension = path.substr(dot + 1);
|
||||
for (char& c : extension) {
|
||||
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c + ('a' - 'A'));
|
||||
}
|
||||
const auto& allowed = *region.allowed_extensions;
|
||||
if (std::find(allowed.begin(), allowed.end(), extension) == allowed.end()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class NativeFileDrop::Target : public IDropTarget {
|
||||
public:
|
||||
Target(HWND view, flutter::BinaryMessenger* messenger)
|
||||
: view_(view), messenger_(messenger) {
|
||||
manager_ = std::make_unique<Channel>(messenger, "cutdex_native/file_drop",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
manager_->SetMethodCallHandler([this](const auto& call, auto result) {
|
||||
const auto* args = call.arguments();
|
||||
const auto* map = args ? std::get_if<Map>(args) : nullptr;
|
||||
if (call.method_name() != "ensureRegion" || !map) {
|
||||
result->NotImplemented(); return;
|
||||
}
|
||||
const auto entry = map->find(Value("id"));
|
||||
const auto* id = entry == map->end() ? nullptr
|
||||
: std::get_if<std::string>(&entry->second);
|
||||
if (!id || id->empty()) {
|
||||
result->Error("invalid_arguments", "Expected a region id"); return;
|
||||
}
|
||||
if (ids_.find(*id) == ids_.end()) {
|
||||
const size_t i = channels_.size();
|
||||
ids_[*id] = i;
|
||||
regions_.emplace_back();
|
||||
channels_.push_back(std::make_unique<Channel>(messenger_,
|
||||
"cutdex_native/file_drop/" + *id,
|
||||
&flutter::StandardMethodCodec::GetInstance()));
|
||||
channels_[i]->SetMethodCallHandler([this, i](const auto& call, auto result) {
|
||||
if (call.method_name() == "setRegion") {
|
||||
regions_[i] = ReadRegion(call.arguments());
|
||||
if (active_ == static_cast<int>(i) &&
|
||||
(!regions_[i] || !Accepts(*regions_[i], files_))) LeaveRegion();
|
||||
result->Success();
|
||||
} else if (call.method_name() == "release") {
|
||||
result->Success();
|
||||
} else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
});
|
||||
}
|
||||
result->Success();
|
||||
});
|
||||
}
|
||||
|
||||
virtual ~Target() = default;
|
||||
|
||||
void Detach() {
|
||||
LeaveRegion();
|
||||
manager_->SetMethodCallHandler(nullptr);
|
||||
for (auto& channel : channels_) channel->SetMethodCallHandler(nullptr);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** object) override {
|
||||
if (!object) return E_POINTER;
|
||||
*object = nullptr;
|
||||
if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IDropTarget)) {
|
||||
*object = static_cast<IDropTarget*>(this);
|
||||
AddRef();
|
||||
return S_OK;
|
||||
}
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
ULONG STDMETHODCALLTYPE AddRef() override { return ++references_; }
|
||||
ULONG STDMETHODCALLTYPE Release() override {
|
||||
const ULONG remaining = --references_;
|
||||
if (remaining == 0) delete this;
|
||||
return remaining;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DragEnter(IDataObject* data, DWORD, POINTL point,
|
||||
DWORD* effect) override {
|
||||
if (!effect) return E_POINTER;
|
||||
Reset();
|
||||
files_ = ReadFiles(data);
|
||||
Update(point, (*effect & DROPEFFECT_COPY) != 0);
|
||||
*effect = active_ >= 0 ? DROPEFFECT_COPY : DROPEFFECT_NONE;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DragOver(DWORD, POINTL point,
|
||||
DWORD* effect) override {
|
||||
if (!effect) return E_POINTER;
|
||||
Update(point, (*effect & DROPEFFECT_COPY) != 0);
|
||||
*effect = active_ >= 0 ? DROPEFFECT_COPY : DROPEFFECT_NONE;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DragLeave() override {
|
||||
Reset();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Drop(IDataObject* data, DWORD, POINTL point,
|
||||
DWORD* effect) override {
|
||||
if (!effect) return E_POINTER;
|
||||
// Confirm the same paths still exist when the user releases the mouse.
|
||||
const auto current = ReadFiles(data);
|
||||
Update(point, (*effect & DROPEFFECT_COPY) != 0 && current == files_);
|
||||
if (active_ >= 0) {
|
||||
Emit("drop");
|
||||
*effect = DROPEFFECT_COPY;
|
||||
// Dart keeps this session until its asynchronous import has completed.
|
||||
// Do not send exit here: asynchronous file preparation can still be pending.
|
||||
active_ = -1;
|
||||
files_.clear();
|
||||
} else {
|
||||
*effect = DROPEFFECT_NONE;
|
||||
Reset();
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
void Update(POINTL point, bool copy_allowed) {
|
||||
POINT client{point.x, point.y};
|
||||
if (!ScreenToClient(view_, &client)) {
|
||||
LeaveRegion();
|
||||
return;
|
||||
}
|
||||
const UINT dpi = FlutterDesktopGetDpiForMonitor(
|
||||
MonitorFromWindow(view_, MONITOR_DEFAULTTONEAREST));
|
||||
const double scale = dpi > 0 ? dpi / 96.0 : 1.0;
|
||||
x_ = client.x / scale;
|
||||
y_ = client.y / scale;
|
||||
int next = -1;
|
||||
if (copy_allowed && !files_.empty()) {
|
||||
for (int i = static_cast<int>(regions_.size()) - 1; i >= 0; --i) {
|
||||
if (regions_[i] && regions_[i]->Contains(x_, y_) &&
|
||||
Accepts(*regions_[i], files_)) {
|
||||
next = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (next != active_) {
|
||||
LeaveRegion();
|
||||
active_ = next;
|
||||
if (active_ >= 0) {
|
||||
++session_;
|
||||
Emit("enter");
|
||||
}
|
||||
} else if (active_ >= 0) {
|
||||
Emit("move");
|
||||
}
|
||||
}
|
||||
|
||||
void Emit(const char* phase) {
|
||||
if (active_ < 0) return;
|
||||
const auto i = static_cast<size_t>(active_);
|
||||
const auto& region = regions_[i];
|
||||
const bool include_files = std::string(phase) == "enter" ||
|
||||
std::string(phase) == "drop";
|
||||
channels_[i]->InvokeMethod("event", std::make_unique<Value>(Map{
|
||||
{Value("phase"), Value(phase)},
|
||||
{Value("session"), Value(session_)},
|
||||
{Value("x"), Value(x_ - (region ? region->x : 0))},
|
||||
{Value("y"), Value(y_ - (region ? region->y : 0))},
|
||||
{Value("files"), Value(include_files ? files_ : List{})},
|
||||
}));
|
||||
}
|
||||
|
||||
void LeaveRegion() {
|
||||
Emit("exit");
|
||||
active_ = -1;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
LeaveRegion();
|
||||
files_.clear();
|
||||
}
|
||||
|
||||
std::atomic<ULONG> references_{1};
|
||||
HWND view_;
|
||||
flutter::BinaryMessenger* messenger_;
|
||||
std::unique_ptr<Channel> manager_;
|
||||
std::map<std::string, size_t> ids_;
|
||||
std::vector<std::unique_ptr<Channel>> channels_;
|
||||
std::vector<std::optional<Region>> regions_;
|
||||
List files_;
|
||||
int active_ = -1;
|
||||
int64_t session_ = 0;
|
||||
double x_ = 0;
|
||||
double y_ = 0;
|
||||
};
|
||||
|
||||
NativeFileDrop::NativeFileDrop(HWND view, flutter::BinaryMessenger* messenger)
|
||||
: view_(view) {
|
||||
// CoInitializeEx alone does not initialize OLE drag-and-drop support.
|
||||
ole_initialized_ = SUCCEEDED(OleInitialize(nullptr));
|
||||
if (!ole_initialized_) {
|
||||
OutputDebugStringW(L"Cutdex: OLE file drop initialization failed.\n");
|
||||
return;
|
||||
}
|
||||
target_ = new Target(view, messenger);
|
||||
registered_ = SUCCEEDED(RegisterDragDrop(view, target_));
|
||||
if (!registered_) {
|
||||
OutputDebugStringW(L"Cutdex: native file drop registration failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
NativeFileDrop::~NativeFileDrop() {
|
||||
if (registered_) RevokeDragDrop(view_);
|
||||
if (target_) {
|
||||
target_->Detach();
|
||||
target_->Release();
|
||||
}
|
||||
if (ole_initialized_) OleUninitialize();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef FLUTTER_PLUGIN_CUTDEX_NATIVE_FILE_DROP_H_
|
||||
#define FLUTTER_PLUGIN_CUTDEX_NATIVE_FILE_DROP_H_
|
||||
|
||||
#include <flutter/binary_messenger.h>
|
||||
#include <windows.h>
|
||||
|
||||
// Receives Explorer file references and routes them to configured Flutter
|
||||
// destinations. Must live on the window's STA thread.
|
||||
class NativeFileDrop {
|
||||
public:
|
||||
NativeFileDrop(HWND view, flutter::BinaryMessenger* messenger);
|
||||
~NativeFileDrop();
|
||||
|
||||
NativeFileDrop(const NativeFileDrop&) = delete;
|
||||
NativeFileDrop& operator=(const NativeFileDrop&) = delete;
|
||||
|
||||
private:
|
||||
class Target;
|
||||
Target* target_ = nullptr;
|
||||
HWND view_;
|
||||
bool ole_initialized_ = false;
|
||||
bool registered_ = false;
|
||||
};
|
||||
|
||||
#endif // FLUTTER_PLUGIN_CUTDEX_NATIVE_FILE_DROP_H_
|
||||
@@ -0,0 +1,265 @@
|
||||
#include "window_chrome.h"
|
||||
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <dwmapi.h>
|
||||
#include <windowsx.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace {
|
||||
using Value = flutter::EncodableValue;
|
||||
using Map = flutter::EncodableMap;
|
||||
const Value* Find(const Map* map, const char* name) {
|
||||
if (!map) return nullptr;
|
||||
const auto it = map->find(Value(name));
|
||||
return it == map->end() ? nullptr : &it->second;
|
||||
}
|
||||
bool Boolean(const Map* map, const char* name, bool fallback = false) {
|
||||
const auto value = Find(map, name);
|
||||
const auto boolean = value ? std::get_if<bool>(value) : nullptr;
|
||||
return boolean ? *boolean : fallback;
|
||||
}
|
||||
double Number(const Map* map, const char* name, double fallback = 0) {
|
||||
const auto value = Find(map, name);
|
||||
if (value) {
|
||||
if (const auto number = std::get_if<double>(value)) return *number;
|
||||
if (const auto number = std::get_if<int32_t>(value)) return *number;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
WindowChrome::WindowChrome(HWND window, HWND view,
|
||||
flutter::BinaryMessenger* messenger)
|
||||
: window_(window), view_(view) {
|
||||
SetWindowSubclass(view_, ViewProc, reinterpret_cast<UINT_PTR>(this),
|
||||
reinterpret_cast<DWORD_PTR>(this));
|
||||
chrome_ = std::make_unique<flutter::MethodChannel<Value>>(
|
||||
messenger, "cutdex_native/window", &flutter::StandardMethodCodec::GetInstance());
|
||||
controls_channel_ = std::make_unique<flutter::MethodChannel<Value>>(
|
||||
messenger, "cutdex_native/window_controls", &flutter::StandardMethodCodec::GetInstance());
|
||||
chrome_->SetMethodCallHandler([this](const auto& call, auto result) {
|
||||
const auto args = call.arguments();
|
||||
const auto map = args ? std::get_if<Map>(args) : nullptr;
|
||||
if (call.method_name() == "configureChrome") {
|
||||
const double height = Number(map, "headerHeight", 46);
|
||||
if (!map || !std::isfinite(height) || height <= 0) {
|
||||
result->Error("invalid_arguments", "Expected a positive header height");
|
||||
return;
|
||||
}
|
||||
Configure(Boolean(map, "enabled"), height,
|
||||
Boolean(map, "interceptClose"), Boolean(map, "headerInteractive", true));
|
||||
result->Success(Value(enabled_));
|
||||
NotifyState();
|
||||
} else if (call.method_name() == "setHeaderInteractive") {
|
||||
const auto active = args ? std::get_if<bool>(args) : nullptr;
|
||||
if (!active) { result->Error("invalid_arguments", "Expected a boolean"); return; }
|
||||
interactive_ = *active;
|
||||
SetMaximizeHovered(false);
|
||||
result->Success();
|
||||
} else if (call.method_name() == "minimize") {
|
||||
ShowWindow(window_, SW_MINIMIZE);
|
||||
result->Success();
|
||||
} else if (call.method_name() == "toggleMaximize") {
|
||||
ShowWindow(window_, IsZoomed(window_) ? SW_RESTORE : SW_MAXIMIZE);
|
||||
result->Success();
|
||||
} else if (call.method_name() == "close") {
|
||||
// Defer destruction until the method reply has reached Flutter.
|
||||
PostMessage(window_, WM_CLOSE, 0, 0);
|
||||
result->Success();
|
||||
} else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
});
|
||||
controls_channel_->SetMethodCallHandler([this](const auto& call, auto result) {
|
||||
if (call.method_name() != "setHeaderControlRegion") { result->NotImplemented(); return; }
|
||||
const auto args = call.arguments();
|
||||
const auto map = args ? std::get_if<Map>(args) : nullptr;
|
||||
const auto id_value = Find(map, "id");
|
||||
const auto id = id_value ? std::get_if<std::string>(id_value) : nullptr;
|
||||
const auto rect_value = Find(map, "rect");
|
||||
if (!id || !rect_value) { result->Error("invalid_arguments", "Expected id and rect"); return; }
|
||||
if (std::holds_alternative<std::monostate>(*rect_value)) {
|
||||
controls_.erase(*id);
|
||||
} else if (const auto rect = std::get_if<Map>(rect_value)) {
|
||||
Region region{Number(rect, "x"), Number(rect, "y"),
|
||||
Number(rect, "width"), Number(rect, "height"), false};
|
||||
const auto role_value = Find(map, "role");
|
||||
const auto role = role_value ? std::get_if<std::string>(role_value) : nullptr;
|
||||
region.maximize = role && *role == "maximize";
|
||||
if (!std::isfinite(region.x) || !std::isfinite(region.y) ||
|
||||
!std::isfinite(region.width) || !std::isfinite(region.height) ||
|
||||
region.width < 0 || region.height < 0) {
|
||||
result->Error("invalid_arguments", "Invalid control bounds"); return;
|
||||
}
|
||||
controls_[*id] = region;
|
||||
} else { result->Error("invalid_arguments", "Invalid rect"); return; }
|
||||
result->Success();
|
||||
});
|
||||
}
|
||||
|
||||
WindowChrome::~WindowChrome() {
|
||||
chrome_->SetMethodCallHandler(nullptr);
|
||||
controls_channel_->SetMethodCallHandler(nullptr);
|
||||
if (IsWindow(view_)) RemoveWindowSubclass(view_, ViewProc, reinterpret_cast<UINT_PTR>(this));
|
||||
}
|
||||
|
||||
void WindowChrome::Configure(bool enabled, double height, bool intercept_close,
|
||||
bool interactive) {
|
||||
// Plugin registration precedes the runner parenting the Flutter view.
|
||||
window_ = GetAncestor(view_, GA_ROOT);
|
||||
const bool changed = enabled_ != enabled;
|
||||
enabled_ = enabled;
|
||||
header_height_ = height;
|
||||
intercept_close_ = intercept_close;
|
||||
interactive_ = interactive;
|
||||
if (!changed) return;
|
||||
if (!enabled) {
|
||||
maximize_pressed_ = false;
|
||||
if (GetCapture() == window_) ReleaseCapture();
|
||||
SetMaximizeHovered(false);
|
||||
}
|
||||
// Retain WS_THICKFRAME, WS_SYSMENU and the caption command styles so Windows
|
||||
// still owns resizing, Alt+Space, double-click zoom and drag-to-snap.
|
||||
const MARGINS margins{0, 0, enabled ? 1 : 0, 0};
|
||||
DwmExtendFrameIntoClientArea(window_, &margins);
|
||||
SetWindowPos(window_, nullptr, 0, 0, 0, 0,
|
||||
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
|
||||
}
|
||||
|
||||
LRESULT WindowChrome::HitTest(LPARAM position) const {
|
||||
POINT point{GET_X_LPARAM(position), GET_Y_LPARAM(position)};
|
||||
RECT window_rect{};
|
||||
GetWindowRect(window_, &window_rect);
|
||||
const UINT dpi = GetDpiForWindow(window_);
|
||||
const int frame = GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) +
|
||||
GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
|
||||
if (!IsZoomed(window_)) {
|
||||
const bool left = point.x < window_rect.left + frame;
|
||||
const bool right = point.x >= window_rect.right - frame;
|
||||
const bool top = point.y < window_rect.top + frame;
|
||||
const bool bottom = point.y >= window_rect.bottom - frame;
|
||||
if (top) return left ? HTTOPLEFT : right ? HTTOPRIGHT : HTTOP;
|
||||
if (bottom) return left ? HTBOTTOMLEFT : right ? HTBOTTOMRIGHT : HTBOTTOM;
|
||||
if (left) return HTLEFT;
|
||||
if (right) return HTRIGHT;
|
||||
}
|
||||
if (!interactive_) return HTCLIENT;
|
||||
ScreenToClient(view_, &point);
|
||||
const double scale = dpi / 96.0;
|
||||
const double x = point.x / scale;
|
||||
const double y = point.y / scale;
|
||||
if (y < 0 || y >= header_height_) return HTCLIENT;
|
||||
// Flutter owns button sizes and positions. Only the reported maximize region
|
||||
// becomes a native hit target for Windows 11 Snap Layouts.
|
||||
LRESULT hit = HTCAPTION;
|
||||
for (const auto& entry : controls_) {
|
||||
const auto& region = entry.second;
|
||||
if (x >= region.x && x < region.x + region.width &&
|
||||
y >= region.y && y < region.y + region.height) {
|
||||
// The caption strip is above workspace controls, including controls
|
||||
// whose layout bounds extend beyond a clipped scroll viewport.
|
||||
if (region.maximize) return HTMAXBUTTON;
|
||||
hit = HTCLIENT;
|
||||
}
|
||||
}
|
||||
return hit;
|
||||
}
|
||||
|
||||
void WindowChrome::NotifyState() {
|
||||
chrome_->InvokeMethod("windowState", std::make_unique<Value>(Map{
|
||||
{Value("maximized"), Value(IsZoomed(window_) != FALSE)},
|
||||
{Value("maximizeHovered"), Value(maximize_hovered_)}}));
|
||||
}
|
||||
|
||||
void WindowChrome::SetMaximizeHovered(bool hovered) {
|
||||
if (maximize_hovered_ == hovered) return;
|
||||
maximize_hovered_ = hovered;
|
||||
NotifyState();
|
||||
}
|
||||
|
||||
std::optional<LRESULT> WindowChrome::HandleMessage(UINT message, WPARAM wparam,
|
||||
LPARAM lparam) {
|
||||
if (!enabled_) return std::nullopt;
|
||||
switch (message) {
|
||||
case WM_NCCALCSIZE: {
|
||||
// Let Windows keep the side/bottom resize borders, replacing only the top.
|
||||
auto rect = wparam ? &reinterpret_cast<NCCALCSIZE_PARAMS*>(lparam)->rgrc[0]
|
||||
: reinterpret_cast<RECT*>(lparam);
|
||||
const LONG top = rect->top;
|
||||
DefWindowProc(window_, message, wparam, lparam);
|
||||
const UINT dpi = GetDpiForWindow(window_);
|
||||
rect->top = top + (IsZoomed(window_) ?
|
||||
GetSystemMetricsForDpi(SM_CYSIZEFRAME, dpi) +
|
||||
GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) : 0);
|
||||
return 0;
|
||||
}
|
||||
case WM_NCHITTEST:
|
||||
return HitTest(lparam);
|
||||
case WM_NCMOUSEMOVE: {
|
||||
SetMaximizeHovered(wparam == HTMAXBUTTON);
|
||||
TRACKMOUSEEVENT tracking{sizeof(TRACKMOUSEEVENT), TME_LEAVE | TME_NONCLIENT,
|
||||
window_, 0};
|
||||
TrackMouseEvent(&tracking);
|
||||
// Continue through DWM/DefWindowProc so Snap Layouts can appear.
|
||||
break;
|
||||
}
|
||||
case WM_NCMOUSELEAVE:
|
||||
SetMaximizeHovered(false);
|
||||
break;
|
||||
case WM_NCLBUTTONDOWN:
|
||||
case WM_NCLBUTTONDBLCLK:
|
||||
if (wparam == HTMAXBUTTON) {
|
||||
maximize_pressed_ = true;
|
||||
SetCapture(window_);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case WM_LBUTTONUP:
|
||||
if (maximize_pressed_) {
|
||||
POINT point{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)};
|
||||
ClientToScreen(window_, &point);
|
||||
const bool activate = HitTest(MAKELPARAM(point.x, point.y)) == HTMAXBUTTON;
|
||||
maximize_pressed_ = false;
|
||||
ReleaseCapture();
|
||||
SetMaximizeHovered(false);
|
||||
if (activate) ShowWindow(window_, IsZoomed(window_) ? SW_RESTORE : SW_MAXIMIZE);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case WM_CAPTURECHANGED:
|
||||
case WM_CANCELMODE:
|
||||
maximize_pressed_ = false;
|
||||
break;
|
||||
case WM_SIZE:
|
||||
NotifyState();
|
||||
break;
|
||||
case WM_CLOSE:
|
||||
if (intercept_close_) {
|
||||
chrome_->InvokeMethod("requestClose", nullptr);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case WM_DWMCOMPOSITIONCHANGED: {
|
||||
const MARGINS margins{0, 0, 1, 0};
|
||||
DwmExtendFrameIntoClientArea(window_, &margins);
|
||||
break;
|
||||
}
|
||||
}
|
||||
LRESULT result = 0;
|
||||
if (DwmDefWindowProc(window_, message, wparam, lparam, &result)) return result;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WindowChrome::ViewProc(HWND window, UINT message, WPARAM wparam,
|
||||
LPARAM lparam, UINT_PTR id, DWORD_PTR data) {
|
||||
const auto chrome = reinterpret_cast<WindowChrome*>(data);
|
||||
if (message == WM_NCHITTEST && chrome->enabled_ &&
|
||||
chrome->HitTest(lparam) != HTCLIENT) {
|
||||
// Flutter is a child HWND covering the client area. Let native caption and
|
||||
// resize hits pass through to its parent while keeping controls in Flutter.
|
||||
return HTTRANSPARENT;
|
||||
}
|
||||
if (message == WM_NCDESTROY) RemoveWindowSubclass(window, ViewProc, id);
|
||||
return DefSubclassProc(window, message, wparam, lparam);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <flutter/binary_messenger.h>
|
||||
#include <flutter/encodable_value.h>
|
||||
#include <flutter/method_channel.h>
|
||||
#include <windows.h>
|
||||
#include <commctrl.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
// Keeps the system resize frame and window commands around a Flutter header.
|
||||
class WindowChrome {
|
||||
public:
|
||||
WindowChrome(HWND window, HWND view, flutter::BinaryMessenger* messenger);
|
||||
~WindowChrome();
|
||||
std::optional<LRESULT> HandleMessage(UINT message, WPARAM wparam, LPARAM lparam);
|
||||
|
||||
private:
|
||||
struct Region {
|
||||
double x, y, width, height;
|
||||
bool maximize;
|
||||
};
|
||||
void Configure(bool enabled, double height, bool intercept_close,
|
||||
bool interactive);
|
||||
LRESULT HitTest(LPARAM position) const;
|
||||
void NotifyState();
|
||||
void SetMaximizeHovered(bool hovered);
|
||||
static LRESULT CALLBACK ViewProc(HWND window, UINT message, WPARAM wparam,
|
||||
LPARAM lparam, UINT_PTR id, DWORD_PTR data);
|
||||
|
||||
HWND window_;
|
||||
HWND view_;
|
||||
bool enabled_ = false;
|
||||
bool intercept_close_ = false;
|
||||
bool interactive_ = true;
|
||||
bool maximize_hovered_ = false;
|
||||
bool maximize_pressed_ = false;
|
||||
double header_height_ = 46;
|
||||
std::map<std::string, Region> controls_;
|
||||
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> chrome_;
|
||||
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> controls_channel_;
|
||||
};
|
||||
Reference in new issue
Block a user