# Mouse-on-roster tkabber plugin.
# Renji <xmpp:rejjin@jabber.dk>

namespace eval mouse-on-roster {
	
	package require msgcat
	::msgcat::mcload [file join [file dirname [info script]] msgs]
	
	variable options
	custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber	
	custom::defgroup Mouse-on-roster [::msgcat::mc "Mouse-on-roster plugin options"] -group Plugins	
	custom::defvar options(enabled) 1 [::msgcat::mc "Enable roster auto hide."] -group Mouse-on-roster -type boolean
	custom::defvar options(width_showed) 150 [::msgcat::mc "Width of showed roster."] -group Mouse-on-roster -type integer
	custom::defvar options(width_hided) 5 [::msgcat::mc "Width of hided roster."] -group Mouse-on-roster -type integer
	custom::defvar options(sleep) 1000 [::msgcat::mc "Sleep of roster hider."] -group Mouse-on-roster -type integer
	
	set options(mouse_on_roster) 0
	set options(showed) 1
	
	trace add variable options(enabled) write \
	[namespace current]::change_state
	
	trace add variable options(width_hided) write \
	[namespace current]::change_width_hided

	trace add variable options(width_showed) write \
	[namespace current]::change_width_showed
	
	if {![::plugins::is_registered mouse-on-roster]} {
		::plugins::register mouse-on-roster \
				-namespace [namespace current] \
				-source [info script] \
				-description [::msgcat::mc "Whether the Mouse-on-roster plugin is loaded"] \
				-loadcommand [namespace code load] \
				-unloadcommand [namespace code unload]
		return
	}
	
	hook::add finload_hook [namespace current]::handle
}

proc mouse-on-roster::load {} {
	variable options
	
	bind .roster <Any-Enter> [namespace current]::show_roster
	bind .roster <Any-Leave> [namespace current]::hide_roster	
	
	if {$options(enabled) == 1} {
		change_width $options(width_hided)
	}
}

proc mouse-on-roster::unload {} {
	variable options
	
	bind .roster <Any-Enter> {}
	bind .roster <Any-Leave> {}
	
	change_width $options(width_showed)
}


proc mouse-on-roster::handle { } {
	variable options
	
	bind .roster <Any-Enter> [namespace current]::show_roster
	bind .roster <Any-Leave> [namespace current]::hide_roster	
	
	if {$options(enabled) == 1} {
		hide_roster
	}
}

proc mouse-on-roster::change_width_hided { who work value } {
	variable options
	if {$options(showed) == 0} {
		change_width $options(width_hided)
	}
}

proc mouse-on-roster::change_width_showed { who work value } {
	variable options
	if {$options(showed) == 1} {
		change_width $options(width_showed)
	}
}

proc mouse-on-roster::change_state { who work value } { 
	variable options
	
	if {$options(enabled) == 0} {
		change_width $options(width_showed)
	} else {
		change_width $options(width_hided)
	}
}

proc mouse-on-roster::show_roster { } {
	variable options
	
	if {$options(enabled) == 0} {
		return
	}
	
	set options(mouse_on_roster) 1
	set options(showed) 1
	
	after idle [list [namespace current]::change_width $options(width_showed)]
}

proc mouse-on-roster::hide_roster { } {
	variable options

	if {$options(enabled) == 0} {
		return
	}
	
	set options(mouse_on_roster) 0
	
	after $options(sleep) [namespace current]::_hide
}

proc mouse-on-roster::_hide { } {
	variable options 
	
	if {$options(mouse_on_roster) == 1} {
		return
	}
	
	change_width $options(width_hided)
	set options(showed) 0
}


proc mouse-on-roster::change_width { x } {
	.mainframe.frame.pw.f0 configure -width $x
}







