Вкладки документов
This commit is contained in:
@@ -1,36 +1,99 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Canvas = @import("Canvas.zig");
|
const Canvas = @import("Canvas.zig");
|
||||||
const CpuRenderEngine = @import("render/CpuRenderEngine.zig");
|
const CpuRenderEngine = @import("render/CpuRenderEngine.zig");
|
||||||
|
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
|
||||||
const Document = @import("models/Document.zig");
|
const Document = @import("models/Document.zig");
|
||||||
|
const basic_models = @import("models/basic_models.zig");
|
||||||
|
|
||||||
const WindowContext = @This();
|
const WindowContext = @This();
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
/// Один открытый документ: документ + свой холст и движок рендера
|
||||||
|
pub const OpenDocument = struct {
|
||||||
|
document: Document,
|
||||||
|
cpu_render: CpuRenderEngine,
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
cpu_render: *CpuRenderEngine,
|
|
||||||
|
/// Инициализировать по месту (canvas хранит указатель на cpu_render этого же экземпляра).
|
||||||
|
pub fn init(allocator: std.mem.Allocator, self: *OpenDocument) void {
|
||||||
|
const default_size = basic_models.Size{ .width = 800, .height = 600 };
|
||||||
|
self.document = Document.init(allocator, default_size);
|
||||||
|
self.cpu_render = CpuRenderEngine.init(allocator, .Squares);
|
||||||
|
self.canvas = Canvas.init(allocator, (&self.cpu_render).renderEngine());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *OpenDocument) void {
|
||||||
|
self.document.deinit();
|
||||||
|
self.canvas.deinit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
frame_index: u64,
|
frame_index: u64,
|
||||||
/// Открытые документы в текущем окне
|
/// Список открытых документов (вкладок): указатели на документ+холст
|
||||||
documents: std.ArrayList(Document),
|
documents: std.ArrayList(*OpenDocument),
|
||||||
|
/// Индекс активной вкладки; null — ни один документ не выбран
|
||||||
|
active_document_index: ?usize,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) !WindowContext {
|
pub fn init(allocator: std.mem.Allocator) !WindowContext {
|
||||||
var self: WindowContext = undefined;
|
const frame_index: u64 = 0;
|
||||||
self.allocator = allocator;
|
const documents = std.ArrayList(*OpenDocument).empty;
|
||||||
|
const active_document_index: ?usize = null;
|
||||||
self.cpu_render = try allocator.create(CpuRenderEngine);
|
return .{
|
||||||
errdefer allocator.destroy(self.cpu_render);
|
.allocator = allocator,
|
||||||
self.cpu_render.* = CpuRenderEngine.init(allocator, .Squares);
|
.frame_index = frame_index,
|
||||||
|
.documents = documents,
|
||||||
self.canvas = Canvas.init(allocator, self.cpu_render.renderEngine());
|
.active_document_index = active_document_index,
|
||||||
|
};
|
||||||
self.frame_index = 0;
|
|
||||||
self.documents = std.ArrayList(Document).empty;
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *WindowContext) void {
|
pub fn deinit(self: *WindowContext) void {
|
||||||
for (self.documents.items) |*doc| doc.deinit();
|
for (self.documents.items) |ptr| {
|
||||||
self.documents.deinit(self.allocator);
|
ptr.deinit();
|
||||||
self.canvas.deinit();
|
self.allocator.destroy(ptr);
|
||||||
self.allocator.destroy(self.cpu_render);
|
}
|
||||||
|
self.documents.deinit(self.allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Вернуть указатель на активный открытый документ (null если нет выбранной вкладки).
|
||||||
|
pub fn activeDocument(self: *WindowContext) ?*OpenDocument {
|
||||||
|
const i = self.active_document_index orelse return null;
|
||||||
|
if (i >= self.documents.items.len) return null;
|
||||||
|
return self.documents.items[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Добавить новый документ и сделать его активным.
|
||||||
|
pub fn addNewDocument(self: *WindowContext) !void {
|
||||||
|
const ptr = try self.allocator.create(OpenDocument);
|
||||||
|
errdefer self.allocator.destroy(ptr);
|
||||||
|
OpenDocument.init(self.allocator, ptr);
|
||||||
|
try self.documents.append(self.allocator, ptr);
|
||||||
|
self.active_document_index = self.documents.items.len - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Выбрать вкладку по индексу.
|
||||||
|
pub fn setActiveDocument(self: *WindowContext, index: usize) void {
|
||||||
|
if (index < self.documents.items.len) {
|
||||||
|
self.active_document_index = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Закрыть вкладку по индексу; активная вкладка сдвигается при необходимости.
|
||||||
|
pub fn closeDocument(self: *WindowContext, index: usize) void {
|
||||||
|
if (index >= self.documents.items.len) return;
|
||||||
|
const open_doc = self.documents.items[index];
|
||||||
|
open_doc.deinit();
|
||||||
|
self.allocator.destroy(open_doc);
|
||||||
|
_ = self.documents.orderedRemove(index);
|
||||||
|
|
||||||
|
if (self.active_document_index) |*active| {
|
||||||
|
if (index < active.*) {
|
||||||
|
active.* -= 1;
|
||||||
|
} else if (index == active.*) {
|
||||||
|
if (self.documents.items.len > 0) {
|
||||||
|
active.* = @min(index, self.documents.items.len - 1);
|
||||||
|
} else {
|
||||||
|
self.active_document_index = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
72
src/main.zig
72
src/main.zig
@@ -3,7 +3,6 @@ const builtin = @import("builtin");
|
|||||||
const dvui = @import("dvui");
|
const dvui = @import("dvui");
|
||||||
const dvui_ext = @import("ui/dvui_ext.zig");
|
const dvui_ext = @import("ui/dvui_ext.zig");
|
||||||
const SDLBackend = @import("sdl-backend");
|
const SDLBackend = @import("sdl-backend");
|
||||||
const Document = @import("models/Document.zig");
|
|
||||||
const ImageRect = @import("models/basic_models.zig").ImageRect;
|
const ImageRect = @import("models/basic_models.zig").ImageRect;
|
||||||
const WindowContext = @import("WindowContext.zig");
|
const WindowContext = @import("WindowContext.zig");
|
||||||
const sdl_c = SDLBackend.c;
|
const sdl_c = SDLBackend.c;
|
||||||
@@ -70,8 +69,8 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gui_frame(ctx: *WindowContext) bool {
|
fn gui_frame(ctx: *WindowContext) bool {
|
||||||
const canvas = &ctx.canvas;
|
|
||||||
const ctrl: bool = dvui.currentWindow().modifiers.control();
|
const ctrl: bool = dvui.currentWindow().modifiers.control();
|
||||||
|
const active_doc = ctx.activeDocument();
|
||||||
|
|
||||||
for (dvui.events()) |*e| {
|
for (dvui.events()) |*e| {
|
||||||
if (e.evt == .window and e.evt.window.action == .close) return false;
|
if (e.evt == .window and e.evt.window.action == .close) return false;
|
||||||
@@ -80,15 +79,39 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
|
|
||||||
const root = dvui.box(
|
const root = dvui.box(
|
||||||
@src(),
|
@src(),
|
||||||
.{ .dir = .horizontal },
|
.{ .dir = .vertical },
|
||||||
.{ .expand = .both, .background = true, .style = .window },
|
.{ .expand = .both, .background = true, .style = .window },
|
||||||
);
|
);
|
||||||
defer root.deinit();
|
defer root.deinit();
|
||||||
|
|
||||||
// Левая панель с фиксированной шириной
|
// Верхняя строка: таббар (вкладки документов + кнопка "Новый")
|
||||||
|
var tab_bar = dvui.box(@src(), .{ .dir = .horizontal }, .{ .expand = .horizontal, .min_size_content = .{ .h = 32 }, .background = true, .padding = dvui.Rect.all(4) });
|
||||||
|
{
|
||||||
|
for (ctx.documents.items, 0..) |open_doc, i| {
|
||||||
|
_ = open_doc;
|
||||||
|
var buf: [32]u8 = undefined;
|
||||||
|
const label = std.fmt.bufPrint(&buf, "Doc {d}", .{i + 1}) catch "Doc";
|
||||||
|
if (dvui.button(@src(), label, .{}, .{ .id_extra = i })) {
|
||||||
|
ctx.setActiveDocument(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dvui.button(@src(), "+", .{}, .{})) {
|
||||||
|
ctx.addNewDocument() catch |err| {
|
||||||
|
std.debug.print("addNewDocument error: {}\n", .{err});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tab_bar.deinit();
|
||||||
|
|
||||||
|
// Нижняя строка: левая панель + контент
|
||||||
|
var content_row = dvui.box(@src(), .{ .dir = .horizontal }, .{ .expand = .both }); // no background to not override
|
||||||
|
{
|
||||||
|
// Левая панель с фиксированной шириной (инструменты для активного документа)
|
||||||
var left_panel = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .vertical, .min_size_content = .{ .w = 200 }, .background = true });
|
var left_panel = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .vertical, .min_size_content = .{ .w = 200 }, .background = true });
|
||||||
{
|
{
|
||||||
dvui.label(@src(), "Tools", .{}, .{});
|
dvui.label(@src(), "Tools", .{}, .{});
|
||||||
|
if (active_doc) |doc| {
|
||||||
|
const canvas = &doc.canvas;
|
||||||
if (dvui.button(@src(), "Fill Random Color", .{}, .{}) or ctx.frame_index == 0) {
|
if (dvui.button(@src(), "Fill Random Color", .{}, .{}) or ctx.frame_index == 0) {
|
||||||
canvas.exampleReset() catch |err| {
|
canvas.exampleReset() catch |err| {
|
||||||
std.debug.print("Error reset example: {}\n", .{err});
|
std.debug.print("Error reset example: {}\n", .{err});
|
||||||
@@ -96,18 +119,21 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
canvas.pos = .{ .x = 400, .y = 400 };
|
canvas.pos = .{ .x = 400, .y = 400 };
|
||||||
}
|
}
|
||||||
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
|
if (dvui.checkbox(@src(), &canvas.native_scaling, "Scaling", .{})) {}
|
||||||
if (dvui.button(@src(), if (ctx.cpu_render.type == .Gradient) "Gradient" else "Squares", .{}, .{})) {
|
if (dvui.button(@src(), if (doc.cpu_render.type == .Gradient) "Gradient" else "Squares", .{}, .{})) {
|
||||||
if (ctx.cpu_render.type == .Gradient) {
|
if (doc.cpu_render.type == .Gradient) {
|
||||||
ctx.cpu_render.type = .Squares;
|
doc.cpu_render.type = .Squares;
|
||||||
} else {
|
} else {
|
||||||
ctx.cpu_render.type = .Gradient;
|
doc.cpu_render.type = .Gradient;
|
||||||
}
|
}
|
||||||
canvas.redrawExample() catch {};
|
canvas.redrawExample() catch {};
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
dvui.label(@src(), "No document", .{}, .{});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
left_panel.deinit();
|
left_panel.deinit();
|
||||||
|
|
||||||
// Правая панель - занимает оставшееся пространство
|
// Правая панель — контент выбранного документа или заглушка
|
||||||
const back = dvui.box(
|
const back = dvui.box(
|
||||||
@src(),
|
@src(),
|
||||||
.{ .dir = .horizontal },
|
.{ .dir = .horizontal },
|
||||||
@@ -127,6 +153,8 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
{
|
{
|
||||||
|
if (active_doc) |doc| {
|
||||||
|
const canvas = &doc.canvas;
|
||||||
var textured = dvui_ext.texturedBox(right_panel.data().contentRectScale(), dvui.Rect.all(20));
|
var textured = dvui_ext.texturedBox(right_panel.data().contentRectScale(), dvui.Rect.all(20));
|
||||||
{
|
{
|
||||||
var overlay = dvui.overlay(
|
var overlay = dvui.overlay(
|
||||||
@@ -150,12 +178,9 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
const natural_scale = if (canvas.native_scaling) 1 else dvui.windowNaturalScale();
|
const natural_scale = if (canvas.native_scaling) 1 else dvui.windowNaturalScale();
|
||||||
const img_size = canvas.getScaledImageSize();
|
const img_size = canvas.getScaledImageSize();
|
||||||
|
|
||||||
// Получить viewport и scroll offset
|
|
||||||
const viewport_rect = scroll.data().contentRect();
|
const viewport_rect = scroll.data().contentRect();
|
||||||
const scroll_current = dvui.Point{ .x = canvas.scroll.viewport.x, .y = canvas.scroll.viewport.y };
|
const scroll_current = dvui.Point{ .x = canvas.scroll.viewport.x, .y = canvas.scroll.viewport.y };
|
||||||
|
|
||||||
// viewport_rect/scroll_current — в natural единицах.
|
|
||||||
// Для расчёта видимой области в пикселях изображения переводим в physical.
|
|
||||||
const viewport_px = dvui.Rect{
|
const viewport_px = dvui.Rect{
|
||||||
.x = viewport_rect.x * natural_scale,
|
.x = viewport_rect.x * natural_scale,
|
||||||
.y = viewport_rect.y * natural_scale,
|
.y = viewport_rect.y * natural_scale,
|
||||||
@@ -171,10 +196,6 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
std.debug.print("updateVisibleImageRect error: {}\n", .{err});
|
std.debug.print("updateVisibleImageRect error: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|
||||||
// `canvas.texture` contains ONLY the visible part.
|
|
||||||
// If we render it inside a widget sized as the full image, dvui will stretch it.
|
|
||||||
// Instead: create a scroll content surface sized like the full image, then place
|
|
||||||
// the visible texture at the correct offset at 1:1.
|
|
||||||
const content_w_px: u32 = img_size.x + img_size.w;
|
const content_w_px: u32 = img_size.x + img_size.w;
|
||||||
const content_h_px: u32 = img_size.y + img_size.h;
|
const content_h_px: u32 = img_size.y + img_size.h;
|
||||||
const content_w = @as(f32, @floatFromInt(content_w_px)) / natural_scale;
|
const content_w = @as(f32, @floatFromInt(content_w_px)) / natural_scale;
|
||||||
@@ -213,7 +234,6 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
}
|
}
|
||||||
canvas_layer.deinit();
|
canvas_layer.deinit();
|
||||||
|
|
||||||
// Заблокировать события скролла, если нажат ctrl
|
|
||||||
if (ctrl) {
|
if (ctrl) {
|
||||||
for (dvui.events()) |*e| {
|
for (dvui.events()) |*e| {
|
||||||
switch (e.evt) {
|
switch (e.evt) {
|
||||||
@@ -242,10 +262,28 @@ fn gui_frame(ctx: *WindowContext) bool {
|
|||||||
overlay.deinit();
|
overlay.deinit();
|
||||||
}
|
}
|
||||||
textured.deinit();
|
textured.deinit();
|
||||||
|
} else {
|
||||||
|
var no_doc_center = dvui.box(
|
||||||
|
@src(),
|
||||||
|
.{ .dir = .vertical },
|
||||||
|
.{ .expand = .both, .padding = dvui.Rect.all(20) },
|
||||||
|
);
|
||||||
|
{
|
||||||
|
dvui.label(@src(), "No document open", .{}, .{});
|
||||||
|
if (dvui.button(@src(), "New document", .{}, .{})) {
|
||||||
|
ctx.addNewDocument() catch |err| {
|
||||||
|
std.debug.print("addNewDocument error: {}\n", .{err});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
no_doc_center.deinit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
right_panel.deinit();
|
right_panel.deinit();
|
||||||
}
|
}
|
||||||
back.deinit();
|
back.deinit();
|
||||||
|
}
|
||||||
|
content_row.deinit();
|
||||||
|
|
||||||
ctx.frame_index += 1;
|
ctx.frame_index += 1;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user