api/ui: use ui options instead of one method per feature

Use new nvim_ui_ prefix to avoid breaking change.
This commit is contained in:
Björn Linse 2016-06-16 16:14:10 +02:00
parent 999af47be8
commit e968d72cae
5 changed files with 80 additions and 20 deletions

View File

@ -8,6 +8,7 @@
#include "nvim/memory.h"
#include "nvim/map.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/api/ui.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/popupmnu.h"
@ -45,8 +46,8 @@ void remote_ui_disconnect(uint64_t channel_id)
xfree(ui);
}
void ui_attach(uint64_t channel_id, Integer width, Integer height,
Boolean enable_rgb, Error *err)
void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height,
Dictionary options, Error *err)
{
if (pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(err, Exception, _("UI already attached for channel"));
@ -58,14 +59,11 @@ void ui_attach(uint64_t channel_id, Integer width, Integer height,
_("Expected width > 0 and height > 0"));
return;
}
UIData *data = xmalloc(sizeof(UIData));
data->channel_id = channel_id;
data->buffer = (Array)ARRAY_DICT_INIT;
UI *ui = xcalloc(1, sizeof(UI));
ui->width = (int)width;
ui->height = (int)height;
ui->rgb = enable_rgb;
ui->data = data;
ui->rgb = true;
ui->pum_external = false;
ui->resize = remote_ui_resize;
ui->clear = remote_ui_clear;
ui->eol_clear = remote_ui_eol_clear;
@ -90,51 +88,107 @@ void ui_attach(uint64_t channel_id, Integer width, Integer height,
ui->set_title = remote_ui_set_title;
ui->set_icon = remote_ui_set_icon;
ui->event = remote_ui_event;
for (size_t i = 0; i < options.size; i++) {
ui_set_option(ui, options.items[i].key, options.items[i].value, err);
if (err->set) {
xfree(ui);
return;
}
}
UIData *data = xmalloc(sizeof(UIData));
data->channel_id = channel_id;
data->buffer = (Array)ARRAY_DICT_INIT;
ui->data = data;
pmap_put(uint64_t)(connected_uis, channel_id, ui);
ui_attach_impl(ui);
return;
}
void ui_detach(uint64_t channel_id, Error *err)
/// @deprecated
void ui_attach(uint64_t channel_id, Integer width, Integer height,
Boolean enable_rgb, Error *err)
{
Dictionary opts = ARRAY_DICT_INIT;
PUT(opts, "rgb", BOOLEAN_OBJ(enable_rgb));
nvim_ui_attach(channel_id, width, height, opts, err);
api_free_dictionary(opts);
}
void nvim_ui_detach(uint64_t channel_id, Error *err)
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(err, Exception, _("UI is not attached for channel"));
return;
}
remote_ui_disconnect(channel_id);
}
Object ui_try_resize(uint64_t channel_id, Integer width,
Integer height, Error *err)
/// @deprecated
void ui_detach(uint64_t channel_id, Error *err)
{
nvim_ui_detach(channel_id, err);
}
void nvim_ui_try_resize(uint64_t channel_id, Integer width,
Integer height, Error *err)
{
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(err, Exception, _("UI is not attached for channel"));
return;
}
if (width <= 0 || height <= 0) {
api_set_error(err, Validation,
_("Expected width > 0 and height > 0"));
return NIL;
return;
}
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
ui->width = (int)width;
ui->height = (int)height;
ui_refresh();
return NIL;
}
void ui_set_popupmenu_external(uint64_t channel_id, Boolean external,
Error *error)
/// @deprecated
void ui_try_resize(uint64_t channel_id, Integer width,
Integer height, Error *err)
{
// Technically this is not needed right now,
// but futher on we might implement smarter behavior when multiple
// ui:s are attached with different draw modes
nvim_ui_try_resize(channel_id, width, height, err);
}
void nvim_ui_set_option(uint64_t channel_id, String name,
Object value, Error *error) {
if (!pmap_has(uint64_t)(connected_uis, channel_id)) {
api_set_error(error, Exception, _("UI is not attached for channel"));
return;
}
UI *ui = pmap_get(uint64_t)(connected_uis, channel_id);
pum_set_external(external);
ui_set_option(ui, name, value, error);
if (!error->set) {
ui_refresh();
}
}
static void ui_set_option(UI *ui, String name, Object value, Error *error) {
if (strcmp(name.data, "rgb") == 0) {
if (value.type != kObjectTypeBoolean) {
api_set_error(error, Validation, _("rgb must be a Boolean"));
return;
}
ui->rgb = value.data.boolean;
} else if (strcmp(name.data, "popupmenu_external") == 0) {
if (value.type != kObjectTypeBoolean) {
api_set_error(error, Validation,
_("popupmenu_external must be a Boolean"));
return;
}
ui->pum_external = value.data.boolean;
} else {
api_set_error(error, Validation, _("No such ui option"));
}
}
static void push_call(UI *ui, char *name, Array args)

View File

@ -83,6 +83,7 @@ UI *tui_start(void)
UI *ui = xcalloc(1, sizeof(UI));
ui->stop = tui_stop;
ui->rgb = p_tgc;
ui->pum_external = false;
ui->resize = tui_resize;
ui->clear = tui_clear;
ui->eol_clear = tui_eol_clear;

View File

@ -27,6 +27,7 @@
#include "nvim/os/time.h"
#include "nvim/os/input.h"
#include "nvim/os/signal.h"
#include "nvim/popupmnu.h"
#include "nvim/screen.h"
#include "nvim/syntax.h"
#include "nvim/window.h"
@ -166,15 +167,18 @@ void ui_refresh(void)
}
int width = INT_MAX, height = INT_MAX;
bool pum_external = true;
for (size_t i = 0; i < ui_count; i++) {
UI *ui = uis[i];
width = ui->width < width ? ui->width : width;
height = ui->height < height ? ui->height : height;
pum_external &= ui->pum_external;
}
row = col = 0;
screen_resize(width, height);
pum_set_external(pum_external);
}
void ui_resize(int new_width, int new_height)

View File

@ -15,7 +15,7 @@ typedef struct {
typedef struct ui_t UI;
struct ui_t {
bool rgb;
bool rgb, pum_external;
int width, height;
void *data;
void (*resize)(UI *ui, int rows, int columns);

View File

@ -31,6 +31,7 @@ UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler)
UIBridgeData *rv = xcalloc(1, sizeof(UIBridgeData));
rv->ui = ui;
rv->bridge.rgb = ui->rgb;
rv->bridge.pum_external = ui->pum_external;
rv->bridge.stop = ui_bridge_stop;
rv->bridge.resize = ui_bridge_resize;
rv->bridge.clear = ui_bridge_clear;