diff --git a/src/DockGuides.cpp b/src/DockGuides.cpp index 066fbcb..87f39d0 100644 --- a/src/DockGuides.cpp +++ b/src/DockGuides.cpp @@ -1,11 +1,12 @@ #include "DockGuides.h" +#include + +#include "DockPlan.h" #include "HostContext.h" namespace dl { -static const int kAreaBandDivisor = 4; - static int ButtonSize() { const int base = LayoutSize("TitleHeaderHeight"); return base > 0 ? base * 2 : 40; @@ -32,31 +33,6 @@ static bool Empty(const RECT& r) { return r.right <= r.left || r.bottom <= r.top; } -static RECT EdgeBand(const RECT& region, int area) { - const int width = (region.right - region.left) / kAreaBandDivisor; - const int height = (region.bottom - region.top) / kAreaBandDivisor; - switch (area) { - case 0: return RECT{ region.left, region.top, region.left + width, region.bottom }; - case 1: return RECT{ region.right - width, region.top, region.right, region.bottom }; - case 2: return RECT{ region.left, region.top, region.right, region.top + height }; - case 3: return RECT{ region.left, region.bottom - height, region.right, region.bottom }; - default: break; - } - return RECT{ region.left, region.top + height, region.right, region.bottom - height }; -} - -static RECT SideHalf(const RECT& r, StackAxis axis, bool after) { - RECT half = r; - if (axis == StackAxis::Vertical) { - const int middle = (r.top + r.bottom) / 2; - (after ? half.top : half.bottom) = middle; - } else { - const int middle = (r.left + r.right) / 2; - (after ? half.left : half.right) = middle; - } - return half; -} - bool CanGroupOnto(const DockModel& model, int source, int target) { const DockPanel* from = model.Get(source); const DockPanel* onto = model.Get(target); @@ -80,13 +56,70 @@ bool CanInsertInto(const DockModel& model, int source, int target) { from->area != kAreaUnknown && from->area == onto->area; } +bool BuildInsertPlan(const DockModel& model, int source, int target, bool after, InsertPlan* plan) { + const DockPanel* from = model.Get(source); + const DockPanel* onto = model.Get(target); + if (!from || !onto || !plan) return false; + + plan->slots.clear(); + for (size_t i = 0; i < model.Panels().size(); i++) + if (model.Panels()[i].stack == from->stack) plan->slots.push_back(static_cast(i)); + for (size_t i = 1; i < plan->slots.size(); i++) + for (size_t j = i; j > 0 && model.Panels()[plan->slots[j - 1]].order > + model.Panels()[plan->slots[j]].order; j--) + std::swap(plan->slots[j - 1], plan->slots[j]); + + int at = -1, to = -1; + for (size_t i = 0; i < plan->slots.size(); i++) { + if (plan->slots[i] == source) at = static_cast(i); + if (plan->slots[i] == target) to = static_cast(i); + } + if (at < 0 || to < 0) return false; + + plan->from = at; + plan->to = after ? (at < to ? to : to + 1) : (at < to ? to - 1 : to); + return plan->to != at; +} + +static bool PlanAreaMove(const DockModel& model, int source, int area, RECT* out) { + const DockPanel* from = model.Get(source); + if (!from || from->grouped) return false; + if (from->area == area) { *out = from->rect; return true; } + + DockPlan plan; + if (!plan.Load(model)) return false; + if (!plan.SetArea(source, area)) return false; + plan.Compute(); + return plan.RectOf(source, out); +} + +static bool PlanGroup(const DockModel& model, int source, int target, RECT* out) { + const DockPanel* from = model.Get(source); + if (!from || from->grouped) return false; + + DockPlan plan; + if (!plan.Load(model)) return false; + if (!plan.Detach(source)) return false; + plan.Compute(); + return plan.RectOf(target, out); +} + +static bool PlanMerge(const DockModel& model, const FloatOrigin& incoming, RECT* out) { + DockPlan plan; + if (!plan.Load(model)) return false; + if (!plan.Add(incoming.left, incoming.top, incoming.right, incoming.bottom, incoming.area)) + return false; + plan.Compute(); + return plan.RectOf(kIncomingPanel, out); +} + void DockGuides::Clear() { buttons_.clear(); hot_ = -1; target_ = DropTarget(); } -void DockGuides::AddAreaCross(const DockModel& model) { +void DockGuides::AddAreaCross(const DockModel& model, int source) { const RECT& region = model.Region(); const POINT center = CenterOf(region); const int size = ButtonSize(); @@ -106,8 +139,8 @@ void DockGuides::AddAreaCross(const DockModel& model) { button.icon = icons[area]; button.target.kind = DropKind::Area; button.target.area = area; - RECT preview = model.AreaRect(area); - button.target.preview = Empty(preview) ? EdgeBand(region, area) : preview; + button.target.known = PlanAreaMove(model, source, area, &button.target.preview); + if (!button.target.known) button.target.preview = RECT{}; buttons_.push_back(button); } } @@ -165,14 +198,32 @@ void DockGuides::AddPanelCluster(const DockModel& model, int source, int hovered button.target.kind = entry.kind; button.target.panel = hovered; button.target.after = entry.after; - button.target.preview = entry.kind == DropKind::Group - ? onto->rect - : SideHalf(onto->rect, axis, entry.after); + if (entry.kind == DropKind::Group) { + button.target.known = PlanGroup(model, source, hovered, &button.target.preview); + if (!button.target.known) button.target.preview = RECT{}; + } + if (entry.kind == DropKind::Insert) { + InsertPlan plan; + if (BuildInsertPlan(model, source, hovered, entry.after, &plan)) { + const DockPanel* slot = model.Get(plan.slots[plan.to]); + if (slot) { + button.target.preview = slot->rect; + button.target.known = true; + } + } else { + const DockPanel* from = model.Get(source); + if (from) { + button.target.preview = from->rect; + button.target.known = true; + } + } + } buttons_.push_back(button); } } -void DockGuides::Update(const DockModel& model, int source, POINT cursor, bool inside) { +void DockGuides::Update(const DockModel& model, int source, POINT cursor, bool inside, + const FloatOrigin* incoming) { buttons_.clear(); hot_ = -1; target_ = DropTarget(); @@ -184,10 +235,12 @@ void DockGuides::Update(const DockModel& model, int source, POINT cursor, bool i button.rect = CenteredRect(CenterOf(model.Region()), ButtonSize()); button.icon = GuideIcon::Merge; button.target.kind = DropKind::Merge; - button.target.preview = model.Region(); + if (incoming && incoming->area != kAreaUnknown) + button.target.known = PlanMerge(model, *incoming, &button.target.preview); + if (!button.target.known) button.target.preview = RECT{}; buttons_.push_back(button); } else { - AddAreaCross(model); + AddAreaCross(model, source); const int hovered = model.IndexAt(cursor); if (hovered >= 0 && hovered != source) AddPanelCluster(model, source, hovered); } diff --git a/src/DockGuides.h b/src/DockGuides.h index 3be6d81..a440573 100644 --- a/src/DockGuides.h +++ b/src/DockGuides.h @@ -4,6 +4,7 @@ #include #include "DockModel.h" +#include "FloatOrigin.h" #include "WindowMenu.h" namespace dl { @@ -20,6 +21,7 @@ struct DropTarget { int area = kAreaUnknown; int panel = -1; bool after = false; + bool known = false; RECT preview = {}; }; @@ -29,9 +31,16 @@ struct GuideButton { DropTarget target; }; +struct InsertPlan { + std::vector slots; + int from = -1; + int to = -1; +}; + class DockGuides { public: - void Update(const DockModel& model, int source, POINT cursor, bool inside); + void Update(const DockModel& model, int source, POINT cursor, bool inside, + const FloatOrigin* incoming); void Clear(); const std::vector& Buttons() const { return buttons_; } @@ -39,7 +48,7 @@ public: const DropTarget& Target() const { return target_; } private: - void AddAreaCross(const DockModel& model); + void AddAreaCross(const DockModel& model, int source); void AddPanelCluster(const DockModel& model, int source, int hovered); std::vector buttons_; @@ -49,5 +58,6 @@ private: bool CanGroupOnto(const DockModel& model, int source, int target); bool CanInsertInto(const DockModel& model, int source, int target); +bool BuildInsertPlan(const DockModel& model, int source, int target, bool after, InsertPlan* plan); } diff --git a/src/DragSession.cpp b/src/DragSession.cpp index e4e33e4..9f3e3b8 100644 --- a/src/DragSession.cpp +++ b/src/DragSession.cpp @@ -1,11 +1,12 @@ #include "DragSession.h" #include -#include #include #include "DockGuides.h" #include "DockModel.h" +#include "DockPlan.h" +#include "FloatOrigin.h" #include "GuideWindow.h" #include "HostContext.h" #include "Log.h" @@ -82,6 +83,20 @@ static POINT ToScreen(HWND window, POINT clientPt) { return pt; } +static void TraceGuides() { + const std::vector& buttons = g_guides.Buttons(); + for (size_t i = 0; i < buttons.size(); i++) { + const GuideButton& button = buttons[i]; + LogTraceF(L"guide %d kind %d button %d %d %d %d known %d preview %d %d %d %d", + static_cast(i), static_cast(button.target.kind), + button.rect.left, button.rect.top, button.rect.right, button.rect.bottom, + button.target.known ? 1 : 0, + button.target.preview.left, button.target.preview.top, + button.target.preview.right, button.target.preview.bottom); + } + LogTraceF(L"guide end hot %d", g_guides.Hot()); +} + bool DragSession::Holding() const { return phase_ == Phase::Armed; } bool DragSession::Dragging() const { return phase_ == Phase::Dragging; } @@ -113,12 +128,15 @@ bool DragSession::BeginFloat(HWND window) { bool DragSession::Start() { panel_ = -1; + hasOrigin_ = false; if (window_ == HostWindow()) { if (!g_model.Build()) return false; panel_ = g_model.IndexAt(origin_); if (panel_ < 0) return false; - } else if (!g_model.BuildRegion()) { - return false; + } else { + const bool built = g_model.Build(); + if (built) hasOrigin_ = FloatOriginOf(window_, &floatOrigin_); + if (!built && !g_model.BuildRegion()) return false; } g_guides.Clear(); @@ -139,7 +157,8 @@ void DragSession::Track(POINT screenPt) { const POINT hostPt = ToHostClient(screenPt); const bool inside = PtInRect(&g_model.Region(), hostPt) != 0; - g_guides.Update(g_model, panel_, hostPt, inside); + g_guides.Update(g_model, panel_, hostPt, inside, hasOrigin_ ? &floatOrigin_ : nullptr); + TraceGuides(); Overlay().Update(g_guides); } @@ -157,40 +176,44 @@ void DragSession::ExecuteGroup(int target) { void DragSession::ExecuteInsert(int target, bool after) { const DockPanel* from = g_model.Get(panel_); - const DockPanel* onto = g_model.Get(target); - if (!from || !onto) return; + InsertPlan plan; + if (!from || !BuildInsertPlan(g_model, panel_, target, after, &plan)) return; - std::vector slots; - for (const DockPanel& panel : g_model.Panels()) - if (panel.stack == from->stack) slots.push_back(&panel); - for (size_t i = 1; i < slots.size(); i++) - for (size_t j = i; j > 0 && slots[j - 1]->order > slots[j]->order; j--) - std::swap(slots[j - 1], slots[j]); - - int at = -1, to = -1; - for (size_t i = 0; i < slots.size(); i++) { - if (slots[i] == from) at = static_cast(i); - if (slots[i] == onto) to = static_cast(i); - } - if (at < 0 || to < 0) return; - - const int destination = after ? (at < to ? to : to + 1) : (at < to ? to - 1 : to); - if (destination == at) return; - - const int step = destination < at ? -1 : 1; + const int step = plan.to < plan.from ? -1 : 1; const WindowCommand command = step < 0 ? WindowCommand::MoveUp : WindowCommand::MoveDown; const POINT grab = { origin_.x - from->rect.left, origin_.y - from->rect.top }; - for (int index = at; index != destination; index += step) { - const RECT& slot = slots[index]->rect; - const POINT point = { min(slot.left + grab.x, slot.right - 1), - min(slot.top + grab.y, slot.bottom - 1) }; + for (int index = plan.from; index != plan.to; index += step) { + const DockPanel* slot = g_model.Get(plan.slots[index]); + if (!slot) return; + const POINT point = { min(slot->rect.left + grab.x, slot->rect.right - 1), + min(slot->rect.top + grab.y, slot->rect.bottom - 1) }; InvokeWindowCommand(HostWindow(), point, command); } LogF(L"ドッキング配置: パネルの並び順を変えました"); } +void DragSession::ExecuteDetach() { + const DockPanel* from = g_model.Get(panel_); + if (!from) return; + + DockPlan plan; + FloatOrigin origin; + const bool known = plan.Load(g_model) && + plan.NormalizedOf(panel_, &origin.left, &origin.top, + &origin.right, &origin.bottom); + origin.area = from->area; + + if (!InvokeWindowCommand(HostWindow(), origin_, WindowCommand::Detach)) return; + LogF(L"ドッキング配置: パネルを分離しました"); + if (known) SetPendingFloat(origin); +} + void DragSession::Execute() { const DropTarget target = g_guides.Target(); + LogTraceF(L"drop kind %d known %d preview %d %d %d %d", + static_cast(target.kind), target.known ? 1 : 0, + target.preview.left, target.preview.top, + target.preview.right, target.preview.bottom); switch (target.kind) { case DropKind::Area: { const DockPanel* from = g_model.Get(panel_); @@ -206,8 +229,7 @@ void DragSession::Execute() { ExecuteInsert(target.panel, target.after); return; case DropKind::Detach: - if (InvokeWindowCommand(HostWindow(), origin_, WindowCommand::Detach)) - LogF(L"ドッキング配置: パネルを分離しました"); + ExecuteDetach(); return; case DropKind::Merge: { RECT client = {}; @@ -219,6 +241,7 @@ void DragSession::Execute() { }; for (const POINT& point : candidates) { if (!InvokeWindowCommand(window_, point, WindowCommand::Merge)) continue; + ForgetFloat(window_); LogF(L"ドッキング配置: ウィンドウを本体へ戻しました"); return; } diff --git a/src/DragSession.h b/src/DragSession.h index c6913d3..e4dc3fd 100644 --- a/src/DragSession.h +++ b/src/DragSession.h @@ -2,6 +2,8 @@ #include +#include "FloatOrigin.h" + namespace dl { class DragSession { @@ -25,6 +27,7 @@ private: void Execute(); void ExecuteGroup(int target); void ExecuteInsert(int target, bool after); + void ExecuteDetach(); void Replay(POINT screenPt); void Reset(); @@ -33,6 +36,8 @@ private: POINT origin_ = {}; int panel_ = -1; bool pending_ = false; + bool hasOrigin_ = false; + FloatOrigin floatOrigin_; }; DragSession& Drag(); diff --git a/src/FloatWindowHook.cpp b/src/FloatWindowHook.cpp index 466a88b..9efe1ec 100644 --- a/src/FloatWindowHook.cpp +++ b/src/FloatWindowHook.cpp @@ -4,6 +4,7 @@ #include #include "DragSession.h" +#include "FloatOrigin.h" #include "HostContext.h" namespace dl { @@ -60,6 +61,7 @@ static LRESULT CALLBACK FloatProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { case WM_NCDESTROY: { if (Drag().Window() == hwnd) Drag().Cancel(); const LRESULT result = CallOriginalProc(hwnd, msg, wp, lp); + ForgetFloat(hwnd); Unhook(hwnd); return result; } @@ -86,6 +88,9 @@ static BOOL CALLBACK Collect(HWND window, LPARAM) { SetWindowProc(window, (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)FloatProc)); g_hooked.push_back(window); + + FloatOrigin origin; + if (TakePendingFloat(&origin)) RememberFloat(window, origin); return TRUE; } diff --git a/src/GuideWindow.cpp b/src/GuideWindow.cpp index a54f3f7..6c954a2 100644 --- a/src/GuideWindow.cpp +++ b/src/GuideWindow.cpp @@ -148,7 +148,7 @@ void Paint(Canvas& canvas, const DockGuides& guides) { const DropTarget& target = guides.Target(); const RECT& preview = target.preview; - if (target.kind != DropKind::None && target.kind != DropKind::Detach && + if (target.kind != DropKind::None && target.known && preview.right > preview.left && preview.bottom > preview.top) { canvas.Fill(preview, accent, kPreviewFillAlpha); canvas.Frame(preview, kPreviewEdgeWidth, accent, kPreviewEdgeAlpha); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 36399be..dcaf188 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -1,6 +1,7 @@ #include #include "DragSession.h" +#include "FloatOrigin.h" #include "FloatWindowHook.h" #include "GuideWindow.h" #include "HostContext.h" @@ -42,6 +43,7 @@ EXTERN_C __declspec(dllexport) void UninitializePlugin() { UninstallFloatWindowHook(); UninstallHostWindowHook(); UninstallWindowMenuHook(); + ClearFloatOrigins(); } BOOL APIENTRY DllMain(HMODULE, DWORD, LPVOID) { return TRUE; }