Вкладки документов
This commit is contained in:
@@ -1,36 +1,99 @@
|
||||
const std = @import("std");
|
||||
const Canvas = @import("Canvas.zig");
|
||||
const CpuRenderEngine = @import("render/CpuRenderEngine.zig");
|
||||
const RenderEngine = @import("render/RenderEngine.zig").RenderEngine;
|
||||
const Document = @import("models/Document.zig");
|
||||
const basic_models = @import("models/basic_models.zig");
|
||||
|
||||
const WindowContext = @This();
|
||||
|
||||
/// Один открытый документ: документ + свой холст и движок рендера
|
||||
pub const OpenDocument = struct {
|
||||
document: Document,
|
||||
cpu_render: CpuRenderEngine,
|
||||
canvas: Canvas,
|
||||
|
||||
/// Инициализировать по месту (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,
|
||||
canvas: Canvas,
|
||||
cpu_render: *CpuRenderEngine,
|
||||
frame_index: u64,
|
||||
/// Открытые документы в текущем окне
|
||||
documents: std.ArrayList(Document),
|
||||
/// Список открытых документов (вкладок): указатели на документ+холст
|
||||
documents: std.ArrayList(*OpenDocument),
|
||||
/// Индекс активной вкладки; null — ни один документ не выбран
|
||||
active_document_index: ?usize,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !WindowContext {
|
||||
var self: WindowContext = undefined;
|
||||
self.allocator = allocator;
|
||||
|
||||
self.cpu_render = try allocator.create(CpuRenderEngine);
|
||||
errdefer allocator.destroy(self.cpu_render);
|
||||
self.cpu_render.* = CpuRenderEngine.init(allocator, .Squares);
|
||||
|
||||
self.canvas = Canvas.init(allocator, self.cpu_render.renderEngine());
|
||||
|
||||
self.frame_index = 0;
|
||||
self.documents = std.ArrayList(Document).empty;
|
||||
|
||||
return self;
|
||||
const frame_index: u64 = 0;
|
||||
const documents = std.ArrayList(*OpenDocument).empty;
|
||||
const active_document_index: ?usize = null;
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.frame_index = frame_index,
|
||||
.documents = documents,
|
||||
.active_document_index = active_document_index,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *WindowContext) void {
|
||||
for (self.documents.items) |*doc| doc.deinit();
|
||||
for (self.documents.items) |ptr| {
|
||||
ptr.deinit();
|
||||
self.allocator.destroy(ptr);
|
||||
}
|
||||
self.documents.deinit(self.allocator);
|
||||
self.canvas.deinit();
|
||||
self.allocator.destroy(self.cpu_render);
|
||||
}
|
||||
|
||||
/// Вернуть указатель на активный открытый документ (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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user