Code Monkey home page Code Monkey logo

Comments (1)

kojix2 avatar kojix2 commented on June 10, 2024

@rubyFeedback

That was a more tedious task than I expected.
I added a red gradient background to the basic_table sample.

image

Here, I added a third column to the table and set the background color.

diff --git a/examples/basic_table.rb b/examples/basic_table.rb
index 474e1d4..fa5d834 100644
--- a/examples/basic_table.rb
+++ b/examples/basic_table.rb
@@ -30,11 +30,17 @@ end
 
 model_handler = UI::FFI::TableModelHandler.malloc
 model_handler.to_ptr.free = Fiddle::RUBY_FREE
-model_handler.NumColumns   = rbcallback(4) { 2 }
-model_handler.ColumnType   = rbcallback(4) { 0 }
+model_handler.NumColumns   = rbcallback(4) { 3 }
+model_handler.ColumnType   = rbcallback(4, [1, 1, 4]) do |_, _, column|
+  column == 2 ? UI::TableValueTypeColor : UI::TableValueTypeString
+end
 model_handler.NumRows      = rbcallback(4) { 5 }
 model_handler.CellValue    = rbcallback(1, [1, 1, 4, 4]) do |_, _, row, column|
-  UI.new_table_value_string(data[row][column])
+  if column == 2
+    UI.new_table_value_color(1, 0, 0, row / 5.0)
+  else
+    UI.new_table_value_string(data[row][column])
+  end
 end
 model_handler.SetCellValue = rbcallback(0, [0]) {}
 
@@ -43,7 +49,7 @@ model = UI.new_table_model(model_handler)
 table_params = UI::FFI::TableParams.malloc
 table_params.to_ptr.free = Fiddle::RUBY_FREE
 table_params.Model = model
-table_params.RowBackgroundColorModelColumn = -1
+table_params.RowBackgroundColorModelColumn = 2
 
 table = UI.new_table(table_params)
 UI.table_append_text_column(table, 'Animal', 0, -1)

Full script.

# frozen_string_literal: true

require 'libui'

UI = LibUI

UI.init

main_window = UI.new_window('Animal sounds', 300, 200, 1)

hbox = UI.new_horizontal_box
UI.window_set_child(main_window, hbox)

data = [
  %w[cat meow],
  %w[dog woof],
  %w[chicken cock-a-doodle-doo],
  %w[horse neigh],
  %w[cow moo]
]

# Protects BlockCaller objects from garbage collection.
@block_callers = []
def rbcallback(*args, &block)
  args << [0] if args.size == 1 # Argument types are ommited
  block_caller = Fiddle::Closure::BlockCaller.new(*args, &block)
  @block_callers << block_caller
  block_caller
end

model_handler = UI::FFI::TableModelHandler.malloc
model_handler.to_ptr.free = Fiddle::RUBY_FREE
model_handler.NumColumns  = rbcallback(4) { 3 }
model_handler.ColumnType  = rbcallback(4, [1, 1, 4]) do |_, _, column|
  column == 2 ? UI::TableValueTypeColor : UI::TableValueTypeString
end
model_handler.NumRows      = rbcallback(4) { 5 }
model_handler.CellValue    = rbcallback(1, [1, 1, 4, 4]) do |_, _, row, column|
  if column == 2
    UI.new_table_value_color(1, 0, 0, row / 5.0)
  else
    UI.new_table_value_string(data[row][column])
  end
end
model_handler.SetCellValue = rbcallback(0, [0]) {}

model = UI.new_table_model(model_handler)

table_params = UI::FFI::TableParams.malloc
table_params.to_ptr.free = Fiddle::RUBY_FREE
table_params.Model = model
table_params.RowBackgroundColorModelColumn = 2

table = UI.new_table(table_params)
UI.table_append_text_column(table, 'Animal', 0, -1)
UI.table_append_text_column(table, 'Description', 1, -1)

UI.box_append(hbox, table, 1)
UI.control_show(main_window)

UI.window_on_closing(main_window) do
  puts 'Bye Bye'
  UI.control_destroy(main_window)
  UI.free_table_model(model)
  UI.quit
  0
end

UI.main
UI.quit

If you want to change the text color instead of the background color, please see here.
https://github.com/AndyObtiva/glimmer-dsl-libui/blob/bc37abfb4134d8ab4e4ddc8f8fd78ce8eb147ce7/lib/glimmer/libui/control_proxy/column/text_color_column_proxy.rb#L42-L44

from libui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.