Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New components #67

Closed
GreenArrow18 opened this issue May 20, 2020 · 2 comments
Closed

New components #67

GreenArrow18 opened this issue May 20, 2020 · 2 comments

Comments

@GreenArrow18
Copy link

local M = component.create("my_component")
The code above creates a brand new my_component. What if I just want to extend the existing functionality of an existing druid component e.g. a button. How can I do this?

Currently, I am using something like this:

function M.init(self, ...)
	self.druid = self:get_druid()
	self.button =  self.druid:new_button(...)
end

but then I have to override all functions like:

function M.set_enabled(self,state)
	self.button:set_enabled(state)
end

Is this the proper way to do things? Or is there a simpler way?

Thanks.

@Insality
Copy link
Owner

Insality commented May 20, 2020

Right now extending components is not designed, but can be done with a little trick.

I will think more about this ability, should be it in Druid or not. Druid prefer composition and customizing components via styles should be enough. If you want replace all buttons, current way is copy button component, rework it and register at druid via druid.register(). Register can override basic components too.

Trick: for now, you can create subclass from druid component in next-way:

local class = require("druid.system.middleclass")
local button = require("druid.base.button")

local M = class("button_custom", button)

--- Class constructor
function M.initialize(self)
	--- Call parent constructor
	button.initialize(self)
end

function M.is_enabled(self)
	print("Overridden function")
	return not self.disabled
end

return M

Thanks for feedback! Issue will be closed after solution for this.

@GreenArrow18
Copy link
Author

I was trying to do it using styles only, but I can't quite figure out how to add some properties to it (for example, save the original node's color so I can manipulate it later).

I wanted to avoid copy/paste of the original button codes, because it pretty much contains the basic functionality already, so for easier code maintenance, I just like to extend it.

Your solution above actually works perfectly for my use case! I set the properties in this new extended component, then use those properties in my new style, without having to mess around further with the original button codes.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants