Skip to content

Feat: add limited mouse support#338

Closed
dzpao wants to merge 2 commits intorivo:masterfrom
dzpao:feat/mouse-support
Closed

Feat: add limited mouse support#338
dzpao wants to merge 2 commits intorivo:masterfrom
dzpao:feat/mouse-support

Conversation

@dzpao
Copy link
Copy Markdown

@dzpao dzpao commented Aug 25, 2019

Add Application.EnableMouse() to enable tcell mouse support.
A limited mouse support is available with #337.

dzpao added 2 commits August 25, 2019 11:16
Add Application.Set(Get)UnknownEventHandler for handling events that tview
does not yet support, such as a mouse event or an user defined event.
@dzpao dzpao force-pushed the feat/mouse-support branch from 2607857 to 5f04651 Compare August 25, 2019 03:18
@millerlogic
Copy link
Copy Markdown
Contributor

Hi, what would make me hesitate to use this is the fact that "unknown" events aren't well defined and could change in the future, thus breaking compatibility. Instead, it could be raw event for all events, or mouse event just for mouse events. I think I like the idea of a raw event handler, it preprocesses all events, and could allow you to intercept and change behavior; and in this case, to receive mouse events.

@millerlogic
Copy link
Copy Markdown
Contributor

It's actually fairly easy to do this without making any changes, wrap the Screen and grab the events:

type MouseScreen struct {
	tcell.Screen
	MouseEventHandler func(event *tcell.EventMouse)
}

func (screen *MouseScreen) PollEvent() tcell.Event {
	event := screen.Screen.PollEvent()
	if mouseEvent, ok := event.(*tcell.EventMouse); ok {
		screen.MouseEventHandler(mouseEvent)
	}
	return event
}

Use:

	app := tview.NewApplication()
	tscreen, err := tcell.NewScreen()
	if err != nil {
		return err
	}
	screen := &MouseScreen{Screen: tscreen,
		MouseEventHandler: func(event *tcell.EventMouse) {
			// ... handle mouse event here ...
		}}
	err = screen.Init()
	if err != nil {
		return err
	}
	screen.EnableMouse()
	app.SetScreen(screen)

@millerlogic millerlogic mentioned this pull request Nov 4, 2019
@dzpao
Copy link
Copy Markdown
Author

dzpao commented Jan 2, 2020

Thanks @millerlogic, I learned a lot from here.

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

Successfully merging this pull request may close these issues.

2 participants