a modern tui library written in zig
18
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(view): fixed several items from PR #82 review

- Removed Window allocation in `init()`.
- Changed `max_width` and `max_height` to `width` and `height` in `Config`.
- Removed `x_pixel` and `y_pixel` from `Config`.
- Changed the `win` parameter of `toWin()` to Type `Window`.
- Fixed a typo in `fill()` so that it's no longer a recursion trap.
- Updated the example w/ corresponding fixes.

authored by

00JCIV00 and committed by rockorager.dev 0dbd72ef ac9839c7

+30 -27
+10 -4
examples/view.zig
··· 68 69 // Initialize Views 70 // - Large Map 71 - var lg_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = lg_map_width, .max_height = lg_map_height }); 72 defer lg_map_view.deinit(); 73 //w = lg_map_view.screen.width; 74 //h = lg_map_view.screen.height; ··· 76 _ = mem.replace(u8, lg_world_map, "\n", "", lg_map_buf[0..]); 77 _ = try lg_map_view.printSegment(.{ .text = lg_map_buf[0..] }, .{ .wrap = .grapheme }); 78 // - Small Map 79 - var sm_map_view = try View.init(alloc, &vx.unicode, .{ .max_width = sm_map_width, .max_height = sm_map_height }); 80 defer sm_map_view.deinit(); 81 w = sm_map_view.screen.width; 82 h = sm_map_view.screen.height; ··· 86 // - Active Map 87 var map_view = lg_map_view; 88 89 - // 90 while (true) { 91 const event = loop.nextEvent(); 92 switch (event) { ··· 164 // a. An x/y (col, row) position within the View as the start point of the render. 165 // b. A Width and Height extending Right and Down from the start point that will represent the square being rendered. 166 // It also returns the calculated x/y position to help maintain scrolloing boundaries. 167 - x, y = try map_view.toWin(&map_win, if (!use_mini_view) .{ 168 .x = x, 169 .y = y, 170 } else .{ ··· 173 .width = .{ .max = 45 }, 174 .height = .{ .max = 15 }, 175 }); 176 177 // Render the screen 178 try vx.render(writer);
··· 68 69 // Initialize Views 70 // - Large Map 71 + var lg_map_view = try View.init(alloc, &vx.unicode, .{ .width = lg_map_width, .height = lg_map_height }); 72 defer lg_map_view.deinit(); 73 //w = lg_map_view.screen.width; 74 //h = lg_map_view.screen.height; ··· 76 _ = mem.replace(u8, lg_world_map, "\n", "", lg_map_buf[0..]); 77 _ = try lg_map_view.printSegment(.{ .text = lg_map_buf[0..] }, .{ .wrap = .grapheme }); 78 // - Small Map 79 + var sm_map_view = try View.init(alloc, &vx.unicode, .{ .width = sm_map_width, .height = sm_map_height }); 80 defer sm_map_view.deinit(); 81 w = sm_map_view.screen.width; 82 h = sm_map_view.screen.height; ··· 86 // - Active Map 87 var map_view = lg_map_view; 88 89 + // TUI Loop 90 while (true) { 91 const event = loop.nextEvent(); 92 switch (event) { ··· 164 // a. An x/y (col, row) position within the View as the start point of the render. 165 // b. A Width and Height extending Right and Down from the start point that will represent the square being rendered. 166 // It also returns the calculated x/y position to help maintain scrolloing boundaries. 167 + x, y = try map_view.toWin(map_win, if (!use_mini_view) .{ 168 .x = x, 169 .y = y, 170 } else .{ ··· 173 .width = .{ .max = 45 }, 174 .height = .{ .max = 15 }, 175 }); 176 + if (use_mini_view) { 177 + _ = try map_win.printSegment( 178 + .{ .text = "This is a mini portion of the View." }, 179 + .{ .row_offset = 16, .col_offset = 5, .wrap = .word }, 180 + ); 181 + } 182 183 // Render the screen 184 try vx.render(writer);
+20 -23
src/View.zig
··· 15 /// Underlying Screen 16 screen: *Screen, 17 /// Underlying Window 18 - win: *Window, 19 20 pub const Config = struct { 21 - max_width: usize = 10_000, 22 - max_height: usize = 10_000, 23 - x_pixel: usize = 0, 24 - y_pixel: usize = 0, 25 }; 26 pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View { 27 const screen = try alloc.create(Screen); 28 screen.* = try Screen.init( 29 alloc, 30 .{ 31 - .cols = config.max_width, 32 - .rows = config.max_height, 33 - .x_pixel = config.x_pixel, 34 - .y_pixel = config.y_pixel, 35 }, 36 unicode, 37 ); 38 - const window = try alloc.create(Window); 39 - window.* = .{ 40 - .x_off = 0, 41 - .y_off = 0, 42 - .width = config.max_width, 43 - .height = config.max_height, 44 - .screen = screen, 45 - }; 46 return .{ 47 .alloc = alloc, 48 .screen = screen, 49 - .win = window, 50 }; 51 } 52 53 pub fn deinit(self: *View) void { 54 - self.alloc.destroy(self.win); 55 self.screen.deinit(self.alloc); 56 self.alloc.destroy(self.screen); 57 } ··· 70 }; 71 /// Render a portion of this View to the provided Window (`win`). 72 /// This will return the bounded X (col), Y (row) coordinates based on the rendering. 73 - pub fn toWin(self: *View, win: *const Window, config: RenderConfig) !struct { usize, usize } { 74 var x = @min(self.screen.width - 1, config.x); 75 var y = @min(self.screen.height - 1, config.y); 76 const width = width: { ··· 105 \\ Position Out of Bounds: 106 \\ - Pos: {d}, {d} 107 \\ - Size: {d}, {d} 108 - , 109 - .{ 110 col, row, 111 self.screen.width, self.screen.height, 112 }, ··· 141 142 /// Fills the View with the provided cell 143 pub fn fill(self: View, cell: Cell) void { 144 - self.fill(cell); 145 } 146 147 /// Prints segments to the View. Returns true if the text overflowed with the
··· 15 /// Underlying Screen 16 screen: *Screen, 17 /// Underlying Window 18 + win: Window, 19 20 + /// View Initialization Config 21 pub const Config = struct { 22 + width: usize, 23 + height: usize, 24 }; 25 + /// Initialize a new View 26 pub fn init(alloc: mem.Allocator, unicode: *const Unicode, config: Config) !View { 27 const screen = try alloc.create(Screen); 28 screen.* = try Screen.init( 29 alloc, 30 .{ 31 + .cols = config.width, 32 + .rows = config.height, 33 + .x_pixel = 0, 34 + .y_pixel = 0, 35 }, 36 unicode, 37 ); 38 return .{ 39 .alloc = alloc, 40 .screen = screen, 41 + .win = .{ 42 + .x_off = 0, 43 + .y_off = 0, 44 + .width = config.width, 45 + .height = config.height, 46 + .screen = screen, 47 + }, 48 }; 49 } 50 51 + /// Deinitialize this View 52 pub fn deinit(self: *View) void { 53 self.screen.deinit(self.alloc); 54 self.alloc.destroy(self.screen); 55 } ··· 68 }; 69 /// Render a portion of this View to the provided Window (`win`). 70 /// This will return the bounded X (col), Y (row) coordinates based on the rendering. 71 + pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { usize, usize } { 72 var x = @min(self.screen.width - 1, config.x); 73 var y = @min(self.screen.height - 1, config.y); 74 const width = width: { ··· 103 \\ Position Out of Bounds: 104 \\ - Pos: {d}, {d} 105 \\ - Size: {d}, {d} 106 + , .{ 107 col, row, 108 self.screen.width, self.screen.height, 109 }, ··· 138 139 /// Fills the View with the provided cell 140 pub fn fill(self: View, cell: Cell) void { 141 + self.win.fill(cell); 142 } 143 144 /// Prints segments to the View. Returns true if the text overflowed with the